使用频率仅次于vector和list
1 #include<iostream> 2 using namespace std; 3 #include<map> 4 5 //map容器 大小和交换 6 7 //大小 8 void test01() 9 { 10 map<int, int>m; 11 m.insert(pair<int, int>(1, 10)); 12 m.insert(pair<int, int>(2, 20)); 13 m.insert(pair<int, int>(3, 30)); 14 15 if (m.empty()) 16 { 17 cout << "m为空" << endl; 18 } 19 else 20 { 21 cout << "m不为空" << endl; 22 cout << "m的大小为:" << m.size() << endl; 23 } 24 25 } 26 27 void printMap(map<int, int>&m) 28 { 29 for (map<int, int>::iterator it = m.begin(); it != m.end(); it++) 30 { 31 cout << "key = " << it->first 32 << " value = " << it->second << endl; 33 } 34 cout << endl; 35 } 36 37 38 //交换 39 void test02() 40 { 41 map<int, int>m; 42 m.insert(pair<int, int>(1, 10)); 43 m.insert(pair<int, int>(2, 20)); 44 m.insert(pair<int, int>(3, 30)); 45 46 map<int, int>m2; 47 m2.insert(pair<int, int>(4, 100)); 48 m2.insert(pair<int, int>(5, 200)); 49 m2.insert(pair<int, int>(6, 300)); 50 51 cout << "交换前:" << endl; 52 printMap(m); 53 printMap(m2); 54 55 cout << "------------------" << endl; 56 cout << "交换后:" << endl; 57 m.swap(m2); 58 printMap(m); 59 printMap(m2); 60 61 } 62 63 int main() 64 { 65 //test01(); 66 test02(); 67 68 return 0; 69 }
1 #include<iostream> 2 using namespace std; 3 #include<map> 4 5 6 //map容器 插入和删除 7 8 void printMap(map<int, int>&m) 9 { 10 for (map<int,int>::iterator it = m.begin(); it != m.end(); it++) 11 { 12 cout << "key = " << it->first 13 << " value = " << it->second << endl; 14 } 15 cout << endl; 16 } 17 18 void test01() 19 { 20 map<int, int>m; 21 22 //插入 23 //第一种 24 m.insert(pair<int, int>(1, 10)); 25 26 //第二种 27 m.insert(make_pair(2, 20)); 28 29 //第三种 30 m.insert(map<int, int>::value_type(3, 30)); //值类型 31 32 //第四种 33 m[4] = 40; //不建议 34 35 //[]不建议插入,可以利用key访问到value 36 //cout << m[4] << endl; 37 38 printMap(m); 39 40 //删除 41 m.erase(m.begin()); 42 printMap(m); 43 44 //m.erase(3); //按照key删除 45 //printMap(m); 46 47 m.erase(30); //按照key删除 48 printMap(m); 49 50 //清空 51 //m.erase(m.begin(), m.end()); 52 m.clear(); 53 printMap(m); 54 55 56 } 57 58 int main() 59 { 60 61 test01(); 62 63 64 return 0; 65 }
1 #include<iostream> 2 using namespace std; 3 #include<map> 4 5 6 //map容器 查找和统计 7 8 //查找 9 void test01() 10 { 11 map<int, int>m; 12 m.insert(pair<int, int>(1, 10)); 13 m.insert(pair<int, int>(2, 20)); 14 m.insert(pair<int, int>(3, 30)); 15 m.insert(pair<int, int>(3, 40)); 16 17 18 map<int,int>::iterator pos = m.find(4); 19 if (pos != m.end()) 20 { 21 cout << "查到了元素key = " << (*pos).first 22 << " value = " << pos->second << endl; 23 } 24 else 25 { 26 cout << "未找到元素" << endl; 27 } 28 29 //统计 30 //map不允许插入重复的key的元素 count统计而言,结果要么是0 要么是1 31 //multimap的count统计可能大于1 32 33 int num = m.count(3); 34 cout << "num = " << num << endl; 35 } 36 37 38 39 40 41 42 int main() 43 { 44 45 test01(); 46 47 48 return 0; 49 }
1 #include<iostream> 2 using namespace std; 3 #include<map> 4 5 class MyCompare 6 { 7 public: 8 bool operator()(int v1,int v2) 9 { 10 //降序 11 return v1 > v2; 12 } 13 }; 14 15 //map容器 排序 16 void test01() 17 { 18 map<int, int, MyCompare>m; 19 20 m.insert(make_pair(1, 10)); 21 m.insert(make_pair(2, 20)); 22 m.insert(make_pair(3, 30)); 23 m.insert(make_pair(4, 40)); 24 m.insert(make_pair(5, 50)); 25 26 for (map<int, int>::iterator it = m.begin(); it != m.end(); it++) 27 { 28 cout << "key = " << it->first 29 << " value = " << it->second << endl; 30 } 31 32 33 34 35 } 36 37 int main() 38 { 39 40 test01(); 41 42 43 return 0; 44 }
自定义数据类型排序:参考set
显示员工信息部分:15:05
看的不太清楚
1 #include<iostream> 2 using namespace std; 3 #include<string> 4 #include<map> 5 #include<vector> 6 #include<ctime> 7 8 #define CEHUA 0 9 #define MEISHU 1 10 #define YANFA 2 11 12 class Worker 13 { 14 public: 15 string m_Name; 16 int m_Salary; 17 18 }; 19 20 void createWorker(vector<Worker>& v) 21 { 22 string nameSeed = "ABCDEFGHIJ"; 23 for (int i = 0; i < 10; i++) 24 { 25 Worker worker; 26 worker.m_Name = "员工"; 27 worker.m_Name += nameSeed[i]; 28 29 worker.m_Salary = rand() % 10000 + 10000;//10000~19999 30 //将员工放入到容器中 31 v.push_back(worker); 32 33 34 } 35 } 36 37 //员工分组 38 void setGroup(vector<Worker> &v, multimap<int, Worker>&m) 39 { 40 for (vector<Worker>::iterator it = v.begin(); it != v.end(); it++) 41 { 42 //产生随机部门编号 43 int deptId = rand() % 3; // 0 1 2 44 45 //将员工插入到分组中 46 //key代表部门编号 value具体员工 47 m.insert(make_pair(deptId, *it)); 48 } 49 50 } 51 52 void showWorkerByGroup(multimap<int, Worker> &m) 53 { 54 //0:A B C; 1:D E; 2:F G…… 55 cout << "策划部门:" << endl; 56 57 multimap<int,Worker>::iterator pos = m.find(CEHUA); 58 int count = m.count(CEHUA); //统计具体人数 59 int index = 0; 60 for (; pos != m.end() && index<count ; pos++,index++) 61 { 62 cout << "姓名:" << pos->second.m_Name 63 << " 工资:" << pos->second.m_Salary << endl; 64 } 65 66 cout << "---------------------------------------" << endl; 67 cout << "美术部门:" << endl; 68 69 pos = m.find(MEISHU); 70 count = m.count(MEISHU); //统计具体人数 71 index = 0; 72 for (; pos != m.end() && index < count; pos++, index++) 73 { 74 cout << "姓名:" << pos->second.m_Name 75 << " 工资:" << pos->second.m_Salary << endl; 76 } 77 78 cout << "---------------------------------------" << endl; 79 cout << "研发部门:" << endl; 80 81 pos = m.find(YANFA); 82 count = m.count(YANFA); //统计具体人数 83 index = 0; 84 for (; pos != m.end() && index < count; pos++, index++) 85 { 86 cout << "姓名:" << pos->second.m_Name 87 << " 工资:" << pos->second.m_Salary << endl; 88 } 89 90 } 91 92 int main() 93 { 94 srand((unsigned int)time(NULL)); 95 //1、创建员工 96 vector<Worker>vWorker; 97 createWorker(vWorker); 98 99 //2、员工分组 100 multimap<int, Worker> mWorker; 101 setGroup(vWorker,mWorker); 102 103 104 //3、分组显示员工 105 showWorkerByGroup(mWorker); 106 107 108 ////测试 109 //for (vector<Worker>::iterator it = vWorker.begin(); it != vWorker.end(); it++) 110 //{ 111 // cout << "姓名:" << it->m_Name 112 // << " 工资:" << it->m_Salary << endl; 113 //} 114 115 116 return 0; 117 }
find返回的是迭代器pos
pos递增之后,去的不是下一个元素?却去了下一个value相同的元素?
合理的解释是返回的迭代器指向的只是 要找的key值对应的那些 对组pair,所以它递增不会乱。
原文:https://www.cnblogs.com/zlh-1024powr/p/14629169.html