一、map/multimap容器
1、map基本概念
map中所有元素都是pair;
pair第一个元素为key(键值),起到索引的作用,第二个元素为value(实值);
所有元素会根据元素的键值(key)自动排序。
map/multimap实质都属于关联式容器 底层结构为二叉树
优点:
可以根据key值快速找到value值
map/multimap区别:
map中不允许有重复的key值,multimap中可以有。
2、构造和赋值
map
map(const map &mp); // 拷贝构造
map& operator=(const map&mp); //赋值重载=操作符
- void printMap(map
int >&mp) -
- {
-
- for (map
int>::iterator it = mp.begin(); it != mp.end(); it++) -
- {
-
- cout << "key = " << (*it).first << " value=" << (*it).second << endl;
-
- }
-
- }
-
-
-
- void test01()
-
- {
-
- map
int>mp; -
- mp.insert(pair
int>("张三", 19)); //插入时必须要先创建一个对组 -
- mp.insert(pair
int>("张四", 20)); //插入时必须要先创建一个对组 -
- mp.insert(pair
int>("张五", 21)); //插入时必须要先创建一个对组 -
- mp.insert(pair
int>("张六", 10)); //插入时必须要先创建一个对组 -
- printMap(mp);
-
- //拷贝构造
-
- map
int>mp1(mp); -
- cout << "mp1:" << endl;
-
- printMap(mp1);
-
-
-
- //赋值
-
- map
int>mp2; -
- mp2 = mp1;
-
- cout << "mp2:" << endl;
-
- printMap(mp2);
-
- }

2、map的大小和交换
函数原型:
size();
empty();
swap(st);
用法与其它容器相同
3、map插入和删除
函数原型:
insert(elem);
clear();
erase(pos);
erase(beg,end);
erase(key); //删除容器中值为key的元素
用法与其他容器相同
- void test02()
-
- {
-
- map
int>mp2; -
- mp2.insert(pair
int>("B", 19)); //插入时必须要先创建一个对组 -
- mp2.insert(pair
int>("C", 20)); //插入时必须要先创建一个对组 -
- mp2.insert(pair
int>("A", 21)); //插入时必须要先创建一个对组 -
- mp2.insert(pair
int>("D", 10)); //插入时必须要先创建一个对组 -
- mp2.insert(make_pair("E", 23));
-
- //第三种插入
-
- mp2.insert(map
int>::value_type("M", 20)); -
- //第四种插入
-
- mp2["F"] = 30;//不建议 ,可以用这个方式访问键值为key的实值value
-
- printMap(mp2);
-
-
-
- //删除
-
- cout << "删除第一个元素" << endl;
-
- mp2.erase(mp2.begin());
-
- printMap(mp2);
-
- cout << "删除key为M的元素" << endl;
-
- mp2.erase("M"); //按key的方式删除
-
- printMap(mp2);
-
- cout << "清空元素" << endl;
-
- mp2.clear();
-
- printMap(mp2);
-
- }

5、map的查找与统计
find(key);//查找key是否存在,若存在 则返回元素的迭代器,不存在,则返回map.end();
count(key);//统计key的元素个数
用法与set的查找与统计相同。
- void test03()
-
- {
-
- map
int>mp2; -
- mp2.insert(pair
int>("B", 19)); //插入时必须要先创建一个对组 -
- mp2.insert(pair
int>("C", 20)); //插入时必须要先创建一个对组 -
- mp2.insert(pair
int>("A", 21)); //插入时必须要先创建一个对组 -
- mp2.insert(pair
int>("D", 10)); //插入时必须要先创建一个对组 -
- mp2.insert(make_pair("E", 23));
-
- //第三种插入
-
- mp2.insert(map
int>::value_type("M", 20)); -
- //第四种插入
-
- mp2["F"] = 30;//不建议 ,可以用这个方式访问键值为key的实值value
-
-
-
- //查找
-
- map
int>::iterator pos = mp2.find("B");//返回的是迭代器 用迭代器接收 -
- if (pos != mp2.end())
-
- {
-
- cout << "元素存在,其实值为:"<< (*pos).second<< endl;
-
- cout << "其个数为: " << mp2.count("B") << endl;
-
- }
-
- else
-
- {
-
- cout << "元素不存在!" << endl;
-
- }
-
- }

6、map容器的排序规则
- class MyCompare {
-
- public:
-
- bool operator()(string v1, string v2)const //重载() 需要加一个const 否则会报错
-
- {
-
- return v1 > v2;
-
- }
-
- };
-
-
-
- void test04()
-
- {
-
- map
int>mp2; -
- mp2.insert(pair
int>("B", 19)); //插入时必须要先创建一个对组 -
- mp2.insert(pair
int>("C", 20)); //插入时必须要先创建一个对组 -
- mp2.insert(pair
int>("A", 21)); //插入时必须要先创建一个对组 -
- mp2.insert(pair
int>("D", 10)); //插入时必须要先创建一个对组 -
- mp2.insert(make_pair("E", 23));
-
- mp2.insert(make_pair("E", 28));
-
- //第三种插入
-
- mp2.insert(map
int>::value_type("M", 20)); -
- //第四种插入
-
- mp2["F"] = 30;//不建议 ,可以用这个方式访问键值为key的实值value
-
-
-
- //查找
-
- map
int>::iterator pos = mp2.find("B");//返回的是迭代器 用迭代器接收 -
- if (pos != mp2.end())
-
- {
-
- cout << "元素存在,其实值为:" << (*pos).second << endl;
-
- cout << "其个数为: " << mp2.count("B") << endl;
-
- }
-
- else
-
- {
-
- cout << "元素不存在!" << endl;
-
- }
-
- cout << "默认排序为:" << endl;
-
- printMap(mp2);
-
- cout << "自己定义排序插入后:" << endl;
-
-
-
- map
int,MyCompare>mp3; -
- mp3.insert(pair
int>("B", 19)); -
- mp3.insert(pair
int>("C", 20)); -
- mp3.insert(pair
int>("A", 21)); -
- mp3.insert(pair
int>("D", 10)); -
- mp3.insert(make_pair("E", 23));
-
- mp3.insert(make_pair("E", 28));
-
- mp3.insert(map
int>::value_type("M", 20)); -
- mp3["F"] = 30;//不建议 ,可以用这个方式访问键值为key的实值value
-
- for (map
int, MyCompare>::iterator it = mp3.begin(); it != mp3.end(); it++) -
- {
-
- cout << "key为: " <
first << " value为:" << (*it).second << endl; -
- }
-
- }

也可以不用定义MyCompare类,直接用greater
- map
int,greater>mp3; -
- mp3.insert(pair
int>("B", 19)); -
- mp3.insert(pair
int>("C", 20)); -
- mp3.insert(pair
int>("A", 21)); -
- mp3.insert(pair
int>("D", 10)); -
- mp3.insert(make_pair("E", 23));
-
- mp3.insert(make_pair("E", 28));
-
- mp3.insert(map
int>::value_type("M", 20)); -
- mp3["F"] = 30;//不建议 ,可以用这个方式访问键值为key的实值value
-
- for (map
int>::iterator it = mp3.begin(); it != mp3.end(); it++) -
- {
-
- cout << "key为: " <
first << " value为:" << (*it).second << endl; -
- }
注意map容器只能通过键值大小来进行排序,不能通过实值的大小来进行排序,若需要通过实值大小排序,可以转为其他容器来实现,如vecter, vecter