C++ STL map/multimap容器
map基本概念:
map中所有元素都是 pair
pair中第一个元素为 key(键值),起到索引作用, 第二个元素为 value(实值)
所有元素都会根据元素的键值自动排序
本质:
map/multimap属于 关联式容器 , 底层结构是用二叉树实现
优点:
可以根据key值快速找到 value值
map 和 multimap 区别
map不允许容器中有重复key值元素
multimap允许容器中有重复key值元素
map构造和赋值
1 #include <iostream> 2 #include <string> 3 #include <map> 4 using namespace std; 5 //map容器 构造和赋值 6 /* 7 构造: 8 map<T1, T2>mp; //map默认构造函数 9 map(const map& mp); //拷贝构造函数 10 赋值: 11 map& operator=(const map& map); //重载等号操作符 12 */ 13 void printMap(map<int, int>& m) 14 { 15 for (map<int, int>::iterator it = m.begin(); it != m.end(); it++) 16 { 17 cout << "key = " << (*it).first << "\tvalue = " << it->second << endl; 18 } 19 cout << endl; 20 } 21 void test() 22 { 23 //创建map容器 24 map<int, int>m;//key vlaue 按照key自动排序 25 m.insert(pair<int, int>(1, 10)); 26 m.insert(pair<int, int>(3, 30)); 27 m.insert(pair<int, int>(4, 40)); 28 m.insert(pair<int, int>(2, 20)); 29 printMap(m); 30 //拷贝构造 31 map<int, int>m2(m); 32 printMap(m2); 33 //赋值 34 map<int, int>m3; 35 m3 = m2; 36 printMap(m3); 37 } 38 int main() 39 { 40 test(); 41 system("pause"); 42 return 0; 43 }
map容器大小和交换
1 #include <iostream> 2 #include <string> 3 #include <map> 4 using namespace std; 5 //统计map容器大小以及交换map容器 6 /* 7 size(); //返回容器中元素的数目 8 empty(); //判断容器是否为空 9 swap(st); //交换两个集合容器 10 */ 11 void printMap(map<int, int>& m) 12 { 13 for (map<int, int>::iterator it = m.begin(); it != m.end(); it++) 14 { 15 cout << "key = " << (*it).first << "\tvalue = " << it->second << endl; 16 } 17 cout << endl; 18 } 19 void test() 20 { 21 map<int, int>m; 22 m.insert(pair<int, int>(1, 10)); 23 m.insert(pair<int, int>(2, 20)); 24 m.insert(pair<int, int>(3, 30)); 25 if (m.empty()) 26 { 27 cout << "map容器为空" << endl; 28 } 29 else 30 { 31 cout << "map容器不为空" << endl; 32 cout <<"map的大小为: " << m.size() << endl; 33 } 34 } 35 void test2() 36 { 37 map<int, int>m1; 38 map<int, int>m2; 39 m1.insert(pair<int, int>(1, 10)); 40 m1.insert(pair<int, int>(2, 20)); 41 m1.insert(pair<int, int>(3, 30)); 42 m2.insert(pair<int, int>(4, 40)); 43 m2.insert(pair<int, int>(5, 50)); 44 m2.insert(pair<int, int>(6, 60)); 45 cout << "交换前:" << endl; 46 cout << "M1:" << endl; 47 printMap(m1); 48 cout << "M2:" << endl; 49 printMap(m2); 50 cout << "交换后:" << endl; 51 m1.swap(m2); 52 cout << "M1:" << endl; 53 printMap(m1); 54 cout << "M2:" << endl; 55 printMap(m2); 56 } 57 int main() 58 { 59 //test(); 60 test2(); 61 system("pause"); 62 return 0; 63 }
map插入和删除
1 #include <iostream> 2 #include <string> 3 #include <map> 4 using namespace std; 5 //map容器进行插入数据和删除数据 6 /* 7 insert(elem); //在容器中插入元素 8 clear(); //清除所有元素 9 erase(pos); //删除pos迭代器所指的元素,返回下一个元素的迭代器 10 erase(beg,end); //删除区间[beg,end)的所有元素,返回下一个元素的迭代器 11 erase(key); //删除容器中值为key的元素 12 */ 13 void printMap(map<int, int>& m) 14 { 15 for (map<int, int>::iterator it = m.begin(); it != m.end(); it++) 16 { 17 cout << "key = " << (*it).first << "\tvalue = " << it->second << endl; 18 } 19 cout << endl; 20 } 21 void test() 22 { 23 map<int, int>m; 24 //插入元素 25 //第一种 26 m.insert(pair<int, int>(1, 10)); 27 //第二种 28 m.insert(make_pair(2, 20)); 29 //第三种 30 m.insert(map<int, int>::value_type(3, 30)); 31 //第四种 32 m[4] = 40; 33 cout << m[5] << endl;//0 会创建出 key = 5 value = 0 的数据 34 //[]不建议插入使用 可以利用key访问到value值 35 cout << m[4] << endl;//40 36 printMap(m); 37 38 //删除 39 m.erase(m.begin());//删除第一个数据 40 printMap(m); 41 m.erase(3);//删除key = 3 的数据 按照key删除 42 printMap(m); 43 m.erase(m.begin(), m.end());//清空 44 m.clear();//清空 45 } 46 int main() 47 { 48 test(); 49 system("pause"); 50 return 0; 51 }
map容器查找和统计
1 #include <iostream> 2 #include <string> 3 #include <map> 4 using namespace std; 5 //对map容器进行查找数据以及统计数据 6 /* 7 find(key); //查找key是否存在,若存在,返回该键的元素的迭代器;若不存在,返回 set.end(); 8 cout(key); //统计key的元素个数 9 */ 10 void printMap(map<int, int>& m) 11 { 12 for (map<int, int>::iterator it = m.begin(); it != m.end(); it++) 13 { 14 cout << "key = " << (*it).first << "\tvalue = " << it->second << endl; 15 } 16 cout << endl; 17 } 18 void test() 19 { 20 //查找 21 map<int, int>m; 22 m.insert(make_pair(1, 10)); 23 m.insert(make_pair(2, 20)); 24 m.insert(make_pair(3, 30)); 25 m.insert(make_pair(3, 50)); 26 map<int,int>::iterator pos = m.find(3);//map中 find函数返回一个迭代器 27 if (pos != m.end()) 28 { 29 cout << "查到了元素" << (*pos).first <<"\t" << pos->second << endl; 30 } 31 else 32 { 33 cout << "没有找到元素" << endl; 34 } 35 //统计 map不允许插入重复key的元素 36 int num = m.count(3);//结果只会为1或者0 multimap的count统计可能大于1 37 cout << "num = " << num << endl; //1 38 } 39 int main() 40 { 41 test(); 42 system("pause"); 43 return 0; 44 }
map容器排序
1 #include <iostream> 2 #include <string> 3 #include <map> 4 using namespace std; 5 //map容器默认排序规则为 按照key值从小到大排序 6 /* 7 利用仿函数,可以改变排序规则 8 */ 9 class MyCompare 10 { 11 public: 12 bool operator()(int v1, int v2) const 13 { 14 return v1 > v2;//降序 15 } 16 }; 17 class Person 18 { 19 public: 20 Person(string name, int age) 21 { 22 this->m_Age = age; 23 this->m_Name = name; 24 } 25 string m_Name; 26 int m_Age; 27 }; 28 void test() 29 { 30 map<int, int,MyCompare>m; 31 m.insert(make_pair(1, 10)); 32 m.insert(make_pair(3, 30)); 33 m.insert(make_pair(2, 20)); 34 for (map<int, int, MyCompare>::iterator it = m.begin(); it != m.end(); it++) 35 { 36 cout << "key = " << (*it).first << "\tvalue = " << it->second << endl; 37 } 38 } 39 void test2() 40 { 41 map<int, Person, MyCompare>m2; 42 Person p1("大", 11); 43 Person p2("二", 22); 44 Person p3("三", 33); 45 m2.insert(make_pair(1, p1)); 46 m2.insert(make_pair(2, p2)); 47 m2.insert(make_pair(3, p3)); 48 for (map<int, Person,MyCompare>::iterator it = m2.begin(); it != m2.end(); it++) 49 { 50 cout << "key = " << it->first << "\t年龄 = " << it->second.m_Age 51 << "\t姓名" << it->second.m_Name << endl; 52 } 53 } 54 int main() 55 { 56 //test(); 57 test2(); 58 system("pause"); 59 return 0; 60 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)