|
map的嵌套与排序规则
- int main(){
- std::cout << "map的嵌套与排序规则" << std::endl;
- // std::map<string, std::map<string, int> > jjj_map;
- // std::map<string, std::map<string, int>, std::less<std::string> > jjj_map;
- std::map<string, std::map<string, int>, std::greater<string> > jjj_map;
- //map 容器存储的都是 pair 类型的键值对元素,更确切的说,该容器存储的都是 pair<const K, T> 类型
- //在使用 map 容器存储多个键值对时,该容器会自动根据各键值对的键的大小,按照既定的规则进行排序
- //可以自定义排序规则
- //map 容器中存储的各个键值对不仅key的值独一无二,键的类型也会用 const 修饰
- jjj_map["c"]["loss"] = 10;
- jjj_map["c"]["num"] = 100;
- jjj_map["b"]["loss"] = 20;
- jjj_map["b"]["num"] = 200;
- jjj_map["a"]["loss"] = 30;
- jjj_map["a"]["num"] = 300;
- //输出当前 jjj_map 容器存储键值对的个数
- cout << "jjj_map size==" << jjj_map.size() << endl;
- for(auto iter = jjj_map.begin(); iter != jjj_map.end(); ++iter){
- // first就是key second就是value
- cout << iter->first << endl;
- for(auto iter2 = iter->second.begin(); iter2 != iter->second.end(); ++iter2){
- cout << iter2->first << "=" << iter2->second << endl;
- }
- }
-
- return 0;
- }
复制代码
|
|