本文共 1746 字,大约阅读时间需要 5 分钟。
#include #include #include #include template void print_map(Map& m){ std::cout << '{ '; for(auto& p : m) std::cout << p.first << ":" << p.second << ' '; std::cout << '}'< map1; map1["something"] = 69; map1["anything"] = 199; map1["that thing"] = 50; std::cout << "map1= "; print_map(map1); //range constructor //从anything到结尾 map iter(map1.find("anything"), map1.end()); cout << "\niter= "; print_map(iter); cout << "map1= "; print_map(map1); //copy construct map copied(map1); cout << "\ncopied= "; print_map(copied); cout << "map1 = "; print_map(map1); //move construct map moved(std::move(map1)); cout< <<"moved = "; print_map(moved); cout<<"map1 = ";print_map(map1); //initalizer list constructor const map init{ { "this",100}, { "can",100}, { "be",100}, { "const",100}, }; cout<<"\ninit = ";print_map(init); //custom key class option 1; //use a comparison struct map mag = { { { 5,-12},13}, { { 3,4},5}, { { 8,-15},17} }; for(auto p:mag) cout<<"The magnitude of("< <<", "< <<") is " < < magy(cmplambda); //various ways of inserting elements: magy.insert(pair ({ 5,-12},13)); magy.insert({ { 3,4},5}); magy.insert({Point{ -8.0,-15.0},17}); cout<<"\n"; for(auto p :magy) { cout<<"The magnitude of { "< <<". "< <<") is " < <<"\n"; } map ::iterator it; //use iterator cout<<'\n'; for(it=magy.begin();it!=magy.end();it++) { cout<<"The magnitude of { "< first.x <<". "< first.y<<") is " < second<<"\n"; } return 0;}
原文地址:https://en.cppreference.com/w/cpp/container/map/map
转载于:https://www.cnblogs.com/shuiyonglewodezzzzz/p/9348472.html