map/multimap容器

使用C++中map/multimap容器时必须包含

#include <map>
using namespace std;

map基本概念

map中所有元素都是pair;
pair中第一个元素为key(键值),起到索引作用,第二个元素为value(实值);
所有元素都会根据元素的键值自动排序;
本质
map/multimap属于关联式容器,底层结构是用二叉树实现的
优点:
可以根据key值快速找到value值
map和multimap区别:
map不允许容器中有重复key值元素
multimap允许容器中有重复key值元素

map构造和赋值

①map<T1,T2> mp;
默认构造函数
②map(const map &m);
拷贝构造函数
③map &operator=(const map &m);
重载=操作符

void printMap(map<int, int> &m) {
    for (map<int, int>::iterator it = m.begin(); it != m.end(); it++) {
        cout << "键值:" << it->first << "实值:" << it->second << endl;
    }
}

void test() {
    map<int, int> m;
    m.insert(pair<int, int>(1, 10)); //匿名对组
    m.insert(pair<int, int>(3, 10));
    m.insert(pair<int, int>(2, 10));
    m.insert(pair<int, int>(4, 10));
    printMap(m);

    map<int, int>m1(m);
    printMap(m1);

    map<int, int> m2;
    m2 = m;
    printMap(m2);
}

map大小和交换

①size();
返回容器中元素的数目
②empty();
判断容器是否为空
③swap(s);
交换两个容器中的元素

void printMap(map<int, int> &m) {
    for (map<int, int>::iterator it = m.begin(); it != m.end(); it++) {
        cout << "键值:" << it->first << "实值:" << it->second << endl;
    }
}

void test() {
    map<int, int> m;
    m.insert(pair<int, int>(1, 10)); //匿名对组
    m.insert(pair<int, int>(3, 10));
    m.insert(pair<int, int>(2, 10));
    m.insert(pair<int, int>(4, 10));

    if (m.empty()) {
        cout << "m为空" << endl;
    }
    else {
        cout << "m不为空" << endl;
        cout << "m的大小" << m.size() << endl;
    }

    map<int, int> m1;
    m1.insert(pair<int, int>(8, 70)); //匿名对组
    m1.insert(pair<int, int>(6, 70));
    m1.insert(pair<int, int>(5, 70));
    m1.insert(pair<int, int>(7, 70));

    m1.swap(m);
    printMap(m);
    printMap(m1);
}

map插入和删除

①insert(elem);
在容器中插入元素
②clear();
清空容器
③erase(pos);
删除pos迭代器所指的元素,返回下一个元素的迭代器
④erase(beg,end);
删除区间[begin,end)的所有元素,返回下一个元素的迭代器
⑤erase(key);
删除容器中键值为key的元素

void printMap(map<int, int> &m) {
    for (map<int, int>::iterator it = m.begin(); it != m.end(); it++) {
        cout << "键值:" << it->first << "实值:" << it->second << endl;
    }
}

void test() {
    map<int, int> m;
    m.insert(pair<int, int>(1, 10)); 
    m.insert(make_pair(2, 20));
    m.insert(map<int, int>::value_type(3, 30));
    m[4] = 40;
    //[]不建议插入,可以利用key访问到value
    printMap(m);

    m.erase(m.begin());
    m.erase(3);
    m.erase(m.begin(), m.end());
    m.clear();
}

map查找和统计

①find(key);
查找key是否存在,若存在,返回该键的元素的迭代器,若不存在,返回set.end();
②count(key);
统计key元素的个数

void test() {
    map<int, int> m;
    m.insert(pair<int, int>(1, 10));
    m.insert(pair<int, int>(3, 10));
    m.insert(pair<int, int>(2, 10));
    m.insert(pair<int, int>(4, 10));

    map<int,int>::iterator pos = m.find(3);
    if (pos != m.end()) {
        cout << "找到了"<<endl;
    }
    else {
        cout << "没找到" << endl;
    }

    int num = m.count(3);
    cout << num << endl;
}

map容器排序

class Mycompare {
public:
    bool operator()(int v1, int v2) {
        return v1 > v2;
    }
};

void test() {
    map<int, int,Mycompare> m;
    m.insert(pair<int, int>(1, 10));
    m.insert(pair<int, int>(3, 10));
    m.insert(pair<int, int>(2, 10));
    m.insert(pair<int, int>(4, 10));

    for (map<int, int, Mycompare>::iterator it = m.begin(); it != m.end(); it++) {
        cout << "键值:" << it->first << "实值:" << it->second << endl;
    }
}

自定义数据类型按键值

class Person {
public:
    Person(string name, int age, int height) {
        this->m_Name = name;
        this->m_Age = age;
        this->m_Height = height;
    }

    string m_Name;
    int m_Age;
    int m_Height;
};

class MyCompare {
public:
    bool operator()(int v1, int v2) {
        return v1 > v2;
    }
};

void test01() {
    map<int, Person,MyCompare> mp;
    Person p1("Tom", 18, 180);
    Person p2("Jerry", 28, 150);
    Person p3("Jeck", 17, 195);

    mp.insert(pair<int, Person>(3, p3));
    mp.insert(make_pair(1, p1));
    mp.insert(make_pair(2, p2));

    for (map<int, Person, MyCompare>::iterator it = mp.begin(); it != mp.end(); it++) {
        cout << "键值" << it->first << "姓名:" << it->second.m_Name << "年龄:" << it->second.m_Age << "身高:" << it->second.m_Height << endl;
    }
}

补充:
在map中键值和实值是映射关系,有以下用法:

map<int, int> m;
m.insert(pair<int,int>(1, 2));
cout << m[1] << endl;

输出为2

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • map/multimap特性 map相对于set区别,map具有键值和实值,所有元素根据键值自动排序。pair的第...
    青春猪头少年_阅读 2,817评论 0 0
  • STL(标准模板库),是目前C++内置支持的library。它的底层利用了C++类模板和函数模板的机制,由三大部分...
    岁与禾阅读 39,328评论 3 132
  • 1. 使用关联容器 2. 关联容器概述2.1 定义关联容器2.2 关键字类型的要求2.3 pair类型 3. 关联...
    MrDecoder阅读 3,395评论 0 0
  • STL部分 1.STL为什么广泛被使用 C++ STL 之所以得到广泛的赞誉,也被很多人使用,不只是提供了像vec...
    杰伦哎呦哎呦阅读 9,810评论 0 9
  • 容器的概念所谓STL容器,即是将最常运用的一些数据结构(data structures)实现出来。容器是指容纳特定...
    饭饭H阅读 2,981评论 0 0

友情链接更多精彩内容