map unordered_map unordered_multimap
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了map unordered_map unordered_multimap,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4070字,纯文字阅读大概需要6分钟。
内容图文
![map unordered_map unordered_multimap](/upload/InfoBanner/zyjiaocheng/1005/6e9849018f214f11aab616bf2701ca61.jpg)
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
内容总结
以上是互联网集市为您收集整理的map unordered_map unordered_multimap全部内容,希望文章能够帮你解决map unordered_map unordered_multimap所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。