-
std::map
- 关联性容器
- key-value存放方式
- 不重复key
-
#include <iostream> #include <map> #include <string> struct Employee { Employee(){} Employee(const std::string& wszName):Name(wszName){} void SetName(std::string newName){ Name = newName;} std::string Name; }; struct ReverseId:public std::binary_function<int, int, bool> { bool operator()(const int& key1, const int& key2) const { return (key1 <= key2) ? false : true; } }; int main() { const int size = 3; const std::pair<int, Employee> items[size] = { std::make_pair(1,Employee("Tom")), std::make_pair(2,Employee("Jerry")), std::make_pair(3,Employee("Alice")), }; std::map<int, Employee, ReverseId> map1(items, items+size); //insert map1.insert(std::make_pair(4, Employee("Brown"))); map1[5] = Employee("Fisher"); //delete std::map<int, Employee, ReverseId>::iterator it = map1.begin(); map1.erase(it); Employee& e = map1[2]; e.SetName("ZhaoHu"); for(it = map1.begin(); it != map1.end(); it++) { std::cout<<"key:"<<it->first<<"value:"<<(it->second).Name<<std::endl; } return 0; }
-
std::multimap
- 可重复key
作者:长风
Email:844064492@qq.com
QQ群:607717453
Git:https://github.com/zhaohu19910409Dz
开源项目:https://github.com/OriginMEK/MEK
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.
感谢您的阅读。如果觉得有用的就请各位大神高抬贵手“推荐一下”吧!你的精神支持是博主强大的写作动力。
如果觉得我的博客有意思,欢迎点击首页左上角的“+加关注”按钮关注我!