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 :

  1. The keys of set and map are unique, but they could be multiple for multiset and multimap.
  2. 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 compiler unordered_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 compiler map<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的更多相关文章

  1. stuff about set multiset map multimap

    A lot of interviewers like to ask the candidates the difference between set and multiset(map and mul ...

  2. STL之六:map/multimap用法详解

    转载于:http://blog.csdn.net/longshengguoji/article/details/8547007 map/multimap 使用map/multimap之前要加入头文件# ...

  3. STL中的map/multimap小结

    (1)使用map/multimap之前必须包含头文件<map>:#include<map> 并且和所有的关联式容器一样,map/multimap通常以平衡二叉树来完成 (2)n ...

  4. STL——容器(Map & multimap)的删除

    Map & multimap 的删除 map.clear();           //删除所有元素 map.erase(pos);      //删除pos迭代器所指的元素,返回下一个元素的 ...

  5. STL——容器(Map & multimap)的大小

    1. Map & multimap 的大小 map.size();     //返回容器中元素的数目 map.empty();//判断容器是否为空, 容器中有内容将会返回 false 代码示例 ...

  6. STL——容器(Map & multimap)的拷贝构造与赋值

    1. Map & multimap 的拷贝构造与赋值 map(const map &mp);               //拷贝构造函数 map& operator=(con ...

  7. STL——容器(Map & multimap)的排序与遍历

    1. Map & multimap 的排序与遍历 map<T1,T2,less<T1> >  mapA; //该容器是按键的升序方式排列元素.如果未指定less< ...

  8. STL——容器(Map & multimap)的插入与迭代器

    1. 容器(Map & multimap)的插入 map.insert(...);    //往容器插入元素,返回pair<iterator,bool> map中插入元素的四种方式 ...

  9. STL——容器(Map & multimap)的简述与构造

    1. map/multimap 的简介 map 是标准的关联式容器,一个 map 里存储的元素是一个键值对序列,叫做 (key,value) 键值对.它提供基于 key 快速检索数据的能力. map ...

随机推荐

  1. ACM——线性表操作

    线性表操作 时间限制(普通/Java):1000MS/3000MS          运行内存限制:65536KByte总提交:2795            测试通过:589 描述 线性表是n个元素 ...

  2. php 5.3 配置mssql笔记

    参考URL  https://docs.moodle.org/29/en/Installing_MSSQL_for_PHP#Using_FreeTDS_on_Debian_Lenny 第一步,下载相应 ...

  3. AVAudioSession 的 AVAudioSessionCategory 和 AVAudioSessionCategoryOptions 相关

    AVAudioSessionCategory相关 AVAudioSessionCategoryAmbient 使用这个category的应用会随着静音键和屏幕关闭而静音.并且不会中止其它应用播放声音, ...

  4. Fxcop 初体验

    代码质量对于软件项目的成败很重要,这点我想大家都明白.那么在一个软件团队中如何保证代码质量呢?对于这个问题不同的人可能会有不同的答案,对于我而言我觉得做好两点代码质量基本就可以保证了: 1.代码规范( ...

  5. Mysql笔记【1】-数据库的基本操作(创建/删除)

    1.创建数据库 创建数据库(如果存在,则报错) #创建名称为test的数据库 create database test 查询创建完的数据库 show databases 2.删除数据库 删除数据库(如 ...

  6. 03_MySQL中文乱码处理_01_MySQl数据库字符集知识

    [MySql数据库常见字符集介绍] 在互联网环境中,使用MySql时常用的字符集有: [如何选择合适的字符集] 1.如果处理各种各样的文字,发布到不同语言的国家地区,应选Unicode字符集,对MyS ...

  7. MateSublg

    MateSublg 说明 使用MetaWeblog的方式提交文章,并自动上传图片. 本插件的官方地址:MateSublg – Sollyu博客 本插件的开源地址:sollyu / MetaSubolg ...

  8. BSTR共享内存问题

    BSTR bstrName = OLESTR("Test String"); BSTR b1 = bstrName; BSTR b2 = bstrName; bstrName = ...

  9. 【BZOJ1042】【DP + 容斥】[HAOI2008]硬币购物

    Description 硬币购物一共有4种硬币.面值分别为c1,c2,c3,c4.某人去商店买东西,去了tot次.每次带di枚ci硬币,买si的价值的东西.请问每次有多少种付款方法. Input 第一 ...

  10. ECMA5.1中关于encodeURI,decodeURI 和encodeComponentURI,decodeComponentURI的区别

    The encodeURI and decodeURI functions are intended to work with complete URIs; theyassume that any r ...