#include #include #include #include #include using namespace std; void createMap(){ //way1 map m0 ; map m1 {{"name","tony"},{"age","20"}}; // map m1 {make_pair("name","tony"),make_pair("age","20")}; //way2 map m2(m1); // //way3 C++ 11 // //创建一个会返回临时 map 对象的函数 // map disMap(){ // std::maptempMap{ {"C语言教程",10},{"STL教程",20} }; // return tempMap; // } // //调用 map 类模板的移动构造函数创建 newMap 容器 // std::map m3(disMap()); //way4 mapm5{ {"C语言教程",10},{"STL教程",20} }; mapnewMap(++m5.begin(), m5.end()); //way5 创建容器同时制定排序规则 std::map m4{ {"C语言教程",10},{"STL教程",20} }; std::map > m4a{ {"C语言教程",10},{"STL教程",20} }; //修改排序规则 std::map > m4b{ {"C语言教程",10},{"STL教程",20} }; } void printMapValue(pair p){ cout << p.first << ":" << p.second << " "; } void mapTraverse(){ map m {{"name","peter"},{"age","19"},{"phone","123456"}}; //way1 for (auto it = m.begin();it != m.cend();++it){ cout << it->first << ":" << it->second << " "; } cout << endl; //way2 auto start = m.begin(); while (start != m.cend()) { cout << start->first << ":" << start->second << " "; start ++; } cout << endl; //way3 for (auto value : m){ cout << value.first << ":" << value.second << " "; } cout << endl; //way4 for_each(m.crbegin(),m.crend(),printMapValue); cout << endl; //way5 find() // auto it = m.find("age"); //找到相当于begin() auto it = m.find("hello"); //没找到就相当于end() for (; it != m.end(); it++) { cout << it->first << ":" << it->second<< " "; } cout << endl; //way6 lower_bound(),upper_bound() //lower_bound(key) 和 upper_bound(key) 更多用于 multimap 容器,在 map 容器中很少用到。 //找到第一个键的值不小于 "name" 的键值对 auto iter = m.lower_bound("name"); cout << "lower:" << iter->first << " " << iter->second << endl; //找到第一个键的值大于 "name" 的键值对 iter = m.upper_bound("name"); cout <<"upper:" << iter->first << " " << iter->second << endl; //创建一个 pair 对象,来接收 equal_range() 的返回值 pair ::iterator, map::iterator> myPair = m.equal_range("name"); //通过遍历,输出 myPair 指定范围内的键值对 for (auto iter = myPair.first; iter != myPair.second; ++iter) { cout << iter->first << " " << iter->second << endl; } } void mapGetValueByKey(){ map mp ; mp["1"] = "C"; mp["2"] = "C++"; mp["3"] = "Go"; mp["4"] = "Ruby"; for (auto value : mp){ cout << value.first << ":" << value.second << " "; } cout << endl; //1)[] 已存在的key为赋值或修改;不存在的key为新增; mp["5"] = "Jave";// mp["4"] = "Js"; for (auto value : mp){ cout << value.first << ":" << value.second << " "; } cout << endl; //2)at() mp.at("5") = "PHP"; // mp.at("6") = "C#";//out_of_range for (auto value : mp){ cout << value.first << ":" << value.second << " "; } cout << endl; // 3)find()或for循环+迭代器 } void mapInsert(){ //1)无需指定插入位置 //创建一个空 map 容器 std::map mp1; //创建一个真实存在的键值对变量 std::pair p1 = { "name","tony" }; //创建一个接收 insert() 方法返回值的 pair 对象 std::pair::iterator, bool> ret; //插入 p1,由于 p1 并不是临时变量,因此会以第一种方式传参 ret = mp1.insert(p1); cout << "ret.iter = <{" << ret.first->first << ", " << ret.first->second << "}, " << ret.second << ">" << endl; //以右值引用的方式传递临时的键值对变量 ret = mp1.insert({ "age","19" }); cout << "ret.iter = <{" << ret.first->first << ", " << ret.first->second << "}, " << ret.second << ">" << endl; //插入失败样例 ret = mp1.insert({ "name","tony" }); cout << "ret.iter = <{" << ret.first->first << ", " << ret.first->second << "}, " << ret.second << ">" << endl; //2)指定插入位置 //创建一个空 map 容器 std::map mp2; //创建一个真实存在的键值对变量 std::pair p2 = { "name","tony" }; //指定要插入的位置 std::map::iterator it = mp2.begin(); //向 it 位置以普通引用的方式插入 p2 auto iter1 = mp2.insert(it, p2); cout << iter1->first << " " << iter1->second << endl; //向 it 位置以右值引用的方式插入临时键值对 auto iter2 = mp2.insert(it, std::pair("age", "19")); cout << iter2->first << " " << iter2->second << endl; //插入失败样例 auto iter3 = mp2.insert(it, std::pair("name", "tony")); cout << iter3->first << " " << iter3->second << endl; //3)插入指定区域的键值对 //创建并初始化 map 容器 std::map mp3{ {"name","tony"}, {"age","19"}, {"phone","123456"} }; //创建一个空 map 容器 std::mapcopymap; //指定插入区域 std::map::iterator first = ++mp3.begin(); std::map::iterator last = mp3.end(); //将区域内的键值对插入到 copymap 中 copymap.insert(first, last); //遍历输出 copymap 容器中的键值对 for (auto iter = copymap.begin(); iter != copymap.end(); ++iter) { cout << iter->first << " " << iter->second << endl; } //4)insert()多个pair map mp4; mp4.insert({{"name","tom"},{"age","17"},{"phone","567890"}}); for (auto iter = mp4.begin(); iter != mp4.end(); ++iter) { cout << iter->first << " " << iter->second << endl; } } void mapEmplace(){ //1.emplace() //创建并初始化 map 容器 std::map mp1; //插入键值对 pair::iterator, bool> ret = mp1.emplace("name", "jemmy"); cout << "1、ret.iter = <{" << ret.first->first << ", " << ret.first->second << "}, " << ret.second << ">" << endl; //插入新键值对 ret = mp1.emplace("age", "15"); cout << "2、ret.iter = <{" << ret.first->first << ", " << ret.first->second << "}, " << ret.second << ">" << endl; //失败插入的样例 ret = mp1.emplace("name", "jemmy"); cout << "3、ret.iter = <{" << ret.first->first << ", " << ret.first->second << "}, " << ret.second << ">" << endl; //2.emplace_hint() //创建并初始化 map 容器 std::mapmp2; //指定在 map 容器插入键值对 map::iterator iter = mp2.emplace_hint(mp2.begin(),"name", "mary"); cout << iter->first << " " << iter->second << endl; iter = mp2.emplace_hint(mp2.begin(), "age", "16"); cout << iter->first << " " << iter->second << endl; //插入失败样例 iter = mp2.emplace_hint(mp2.begin(), "name", "mary"); cout << iter->first << " " << iter->second << endl; } int main(){ //mapTraverse(); // mapGetValueByKey(); // mapInsert(); mapEmplace(); return 0; }
上一篇 数据结构-顺序表的合并
下一篇 公安机关互联网安全监督规定检查规定解决方案
版权所有 (c)2021-2022 MSHXW.COM
ICP备案号:晋ICP备2021003244-6号