STL set multiset map multimap unordered_set unordered_map example
来源:互联网 发布:溜冰鞋淘宝网 编辑:程序博客网 时间:2024/06/05 15:38
I decide to write to my blogs in English. When I meet something hard to depict, I'll add some Chinese necessarily.
The differences between these containers are :
- The keys of set and map are unique, but they could be multiple for multiset and multimap.
- Unordered_set and unordered_map are implemented by hash map, but the above 4 containers are implemented by Red-Black tree. They could have been named as hash_map, however, these names seem to have be used by other standards. So this ugly name ‘unordered’ is adopted in contrast to the attribute 'ordered' of the above 4 containers.
The following is the code:
#include<map>#include<stdio.h>#include<unordered_map> #include<algorithm>//#include<unordered_multimap> //there isn't such a library#include<string>#include<iostream>using namespace std;int main(){unordered_multimap<int, string> mapStudent1;mapStudent1.insert(pair<int, string>(1, "student_one"));mapStudent1.insert(pair<int, string>(2, "student_two"));mapStudent1.insert(unordered_multimap<int, string>::value_type (2, "student_two")); //这样插入也可以,但是注意key是Unique的 mapStudent1.insert(make_pair<int, string>(3, "student_three"));mapStudent1.insert(make_pair<int, string>(3, "student_three"));//mapStudent.emplace(5,"student_five"); //Have to add the command '-std=c++11' for compilerunordered_multimap<int, string>::iterator iter1;for(iter1 = mapStudent1.begin(); iter1 != mapStudent1.end(); iter1++){cout<<iter1->first<<" "<<iter1->second<<endl;}printf("-------------------------------\n\n");map<int, string> mapStudent2;mapStudent2.insert(pair<int, string>(1, "student_one"));mapStudent2.insert(pair<int, string>(2, "student_two"));mapStudent2.insert(map<int, string>::value_type (2, "student_two")); //这样插入也可以,但是注意key是Unique的 mapStudent2.insert(pair<int, string>(3, "student_three"));mapStudent2.insert(pair<int, string>(3, "student_three"));mapStudent2[4]="hello"; //利用数组插入同样可以,但是效率比较低 //mapStudent.emplace(5,"student_five"); //Have to add the command '-std=c++11' for compilermap<int, string>::iterator iter2;for(iter2 = mapStudent2.begin(); iter2 != mapStudent2.end(); iter2++){cout<<iter2->first<<" "<<iter2->second<<endl;}printf("-------------------------------\n\n"); std::unordered_multimap<std::string,std::string> myumm = { {"orange","FL"}, {"strawberry","LA"}, {"strawberry","OK"}, {"pumpkin","NH"} }; for (auto& x: {"orange","lemon","strawberry"}) { std::cout << x << ": " << myumm.count(x) << " entries.\n"; } printf("-------------------------------\n\n");typedef std::unordered_multimap<std::string,std::string> stringmap; stringmap myumm1 = { {"orange","FL"}, {"strawberry","LA"}, {"pumpkin","NH"}, {"strawberry","OK"} };cout<<"All entries are:"<<endl;stringmap::iterator iter3;for(iter3 = myumm1.begin(); iter3 != myumm1.end(); iter3++){cout<<iter3->first<<" "<<iter3->second<<endl;} std::cout << "Entries with strawberry:"; auto range = myumm1.equal_range("strawberry"); for_each ( range.first, range.second, [](stringmap::value_type& x){std::cout << " " << x.second;} );return 0;}
- STL set multiset map multimap unordered_set unordered_map example
- map,multimap,unordered_map,set,multiset,unordered_set
- STL中map,multimap,set,multiset,unordered_map,unordered_multimap,unordered_set,unordered_multiset的实现方法
- STL中map,multimap,set,multiset,unordered_map,unordered_multimap,unordered_set,unordered_multiset的实现方
- map/multimap/unordered_map/unordered_multimap; set/multiset/unordered_set/unordered_multiset
- 关联容器(map、set、multimap、multiset、pair、unordered_map)
- STL map, multimap, set, multiset 函数介绍
- STL容器 set、 multiset、map、multimap
- C++ STL之 map\set\multimap\multiset
- map,multimap,set,multiset
- set,map,multiset,multimap
- map,multimap,set,multiset
- map/multimap/set/multiset
- 【STL】STL中红黑树的应用set、multiset、map、multimap
- STL概览-关联容器set,multiset,map,multimap(四)
- STL源码剖析之map set multimap multiset【2013.12.10】
- stl中的map、set、multimap、multiset,兼谈OceanBase造轮子
- STL中HashTable HashMap HashSet Set Map MultiSet MultiMap总结
- openfire3.7插件开发方法
- VS2010安装项目程序打包操作详解
- 疯狂java 手册~~
- FMDatabase的用法介绍
- Oracle 11G函数整理(转换函数)
- STL set multiset map multimap unordered_set unordered_map example
- Trie树实现三----双数组trie树
- hadoop配置fair-scheduler的方法
- 《苹果的产品设计之道》摘录
- Python sh库学习 上篇
- MySQL 分页查询
- 移动开发者自白:我为什么从PhoneGap中逃离
- MySQL单机多实例方案
- win7搭建hadoop集群