C++ map容器插入具有相同键的键值对的覆盖问题
map容器插入键值对的方法一般有两种
map["key"] = value;
map.insert(make_pair<>("", ""));
#include <map> #include <iostream> using namespace std; int main() { map<double, double> mp1; mp1[1.0] = 1.1; mp1[1.0] = 1.2; cout << mp1.size() << endl; cout << mp1.begin()->second << endl; return 0; }
输出结果为
1 1.2
使用方法二插入相同键的键值对时,后一组的键值对不会插入map容器,即不会覆盖前一组键值对。代码如下:
#include <map> #include <iostream> using namespace std; int main() { map<double, double> mp1; mp1.insert(make_pair<double, double>(2.0, 2.1)); mp1.insert(make_pair<double, double>(2.0, 2.2)); cout << mp1.size() << endl; cout << mp1.begin()->second << endl; return 0; }
输出结果为
1 2.1
#include <string> #include <iostream> //查询性能最高 //允许重复的,hash_map #include <unordered_map> #include <algorithm> using namespace std; int main() { //允许重复的映射 unordered_multimap<string, double>mymap{ {"a1",113},{ "a2",143 },{ "a3",1123 } }; mymap.insert(pair<string, double>("a4", 345)); mymap.insert(pair<string, double>("a4", 315)); mymap.insert(pair<string, double>("a4", 325)); mymap.insert(pair<string, double>("a4", 335)); /*mymap.insert(unordered_multimap<string, double>::value_type("a5", 3425));*/ /*for (auto i : mymap) { cout << i.first << " " << i.second << endl; }*/ /*auto it = mymap.find("a1"); if (it != mymap.end()) { cout << it->second << endl; }*/ //查找所有 auto it = mymap.equal_range("a4"); for_each(it.first, it.second, [](unordered_multimap<string, double>::value_type &x) {cout << x.first << " " << x.second << endl; }); cin.get(); return 0; }
root@ubuntu:~/c++# g++ -std=c++11 multimap.cpp -o multimap root@ubuntu:~/c++# ./multimap a4 335 a4 325 a4 315 a4 345
#include <string> #include <iostream> //查询性能最高 //允许重复的,hash_map #include <unordered_map> #include <algorithm> using namespace std; int main() { //允许重复的映射 unordered_map<string, double>mymap{ {"a1",113},{ "a2",143 },{ "a3",1123 } }; mymap.insert(pair<string, double>("a4", 345)); mymap.insert(pair<string, double>("a4", 315)); mymap.insert(pair<string, double>("a4", 325)); mymap.insert(pair<string, double>("a4", 335)); /*mymap.insert(unordered_multimap<string, double>::value_type("a5", 3425));*/ /*for (auto i : mymap) { cout << i.first << " " << i.second << endl; }*/ /*auto it = mymap.find("a1"); if (it != mymap.end()) { cout << it->second << endl; }*/ //查找所有 auto it = mymap.equal_range("a4"); for_each(it.first, it.second, [](unordered_multimap<string, double>::value_type &x) {cout << x.first << " " << x.second << endl; }); cin.get(); return 0; }
root@ubuntu:~/c++# g++ -std=c++11 multimap.cpp -o multimap root@ubuntu:~/c++# ./multimap a4 345 root@ubuntu:~/c++# cat multimap.cpp
#include <unordered_map> #include <string> #include<iostream> using namespace std; class Node { public: Node(int age, string name); ~Node(); bool operator==(const Node &n) const; public: std::string m_strName; int m_iAge; }; Node::Node(int age, string name) :m_strName(name),m_iAge(age) { } Node::~Node() { } bool Node::operator==(const Node & n) const { if (n.m_iAge==m_iAge && m_strName==n.m_strName) { return true; } return false; } struct KeyHasher { std::size_t operator()(const Node& k) const { using std::size_t; using std::hash; using std::string; return ((hash<string>()(k.m_strName)) ^ (hash<int>()(k.m_iAge) << 1)); } }; int main( ) { std::unordered_map<Node, int, KeyHasher> myMap; myMap.insert(pair<Node, int>(Node(24, "kobe"), 24)); //遍历输出+迭代器的使用 auto iter = myMap.begin();//auto自动识别为迭代器类型unordered_map<int,string>::iterator while (iter!= myMap.end()) { cout << iter->second << endl; ++iter; } auto iterator = myMap.find(Node(24, "kobe"));//find()返回一个指向2的迭代器 if (iterator != myMap.end()) cout << iterator->first.m_strName << " "<< iterator->second << endl; myMap.insert(pair<Node, int>(Node(24, "kobe"), 40)); iterator = myMap.find(Node(24, "kobe"));//find()返回一个指向2的迭代器 if (iterator != myMap.end()) cout << iterator->first.m_strName << " "<< iterator->second << endl; }
root@ubuntu:~/c++# ./unorder2 24 kobe 24 kobe 24