STL set multiset map multimap unordered_set unordered_map example
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.
#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的更多相关文章
- stuff about set multiset map multimap
A lot of interviewers like to ask the candidates the difference between set and multiset(map and mul ...
- STL之六:map/multimap用法详解
转载于:http://blog.csdn.net/longshengguoji/article/details/8547007 map/multimap 使用map/multimap之前要加入头文件# ...
- STL中的map/multimap小结
(1)使用map/multimap之前必须包含头文件<map>:#include<map> 并且和所有的关联式容器一样,map/multimap通常以平衡二叉树来完成 (2)n ...
- STL——容器(Map & multimap)的删除
Map & multimap 的删除 map.clear(); //删除所有元素 map.erase(pos); //删除pos迭代器所指的元素,返回下一个元素的 ...
- STL——容器(Map & multimap)的大小
1. Map & multimap 的大小 map.size(); //返回容器中元素的数目 map.empty();//判断容器是否为空, 容器中有内容将会返回 false 代码示例 ...
- STL——容器(Map & multimap)的拷贝构造与赋值
1. Map & multimap 的拷贝构造与赋值 map(const map &mp); //拷贝构造函数 map& operator=(con ...
- STL——容器(Map & multimap)的排序与遍历
1. Map & multimap 的排序与遍历 map<T1,T2,less<T1> > mapA; //该容器是按键的升序方式排列元素.如果未指定less< ...
- STL——容器(Map & multimap)的插入与迭代器
1. 容器(Map & multimap)的插入 map.insert(...); //往容器插入元素,返回pair<iterator,bool> map中插入元素的四种方式 ...
- STL——容器(Map & multimap)的简述与构造
1. map/multimap 的简介 map 是标准的关联式容器,一个 map 里存储的元素是一个键值对序列,叫做 (key,value) 键值对.它提供基于 key 快速检索数据的能力. map ...
随机推荐
- html-01
1.HTML:超文本标记语言,由浏览器解析成页面.html文件是以.html或者 .htm 2.HTML的作用 |- 控制页面的外观. |- 发布帮助文档 3.常见的浏览器 |-IE:微 ...
- java.lang.IllegalStateException: You need to use a theme.appcompat theme (or descendant) with this activity
错误描述:java.lang.IllegalStateException: You need to use a theme.appcompat theme (or descendant) with t ...
- C#删除微信自定义菜单
删除 string access_token = "你的token"; string posturl = "https://api.weixin.qq.com/cgi-b ...
- WPF动画之线性插值动画(1)
XAML代码: <Window x:Class="线性插值动画.MainWindow" xmlns="http://schemas.microsoft.com/wi ...
- nvmw install 失败. 需修改"Msxml2.XMLHTTP"为"Msxml2.ServerXMLHTTP"
准备在windows下学习nodejs. 下载了nvmw . 但没法安装node的任何版本. 都是报错如下: C:\Users\WXG>nvmw install v0.12.0 x86 Star ...
- 03_JqueryAjax_异步请求Servlet
[Ajax 简述] jquery对Ajax提供了更方便的代码:$ajax({ops})来发送异步请求. 首先说一个Ajax的特性,它是永安里发送异步请求,请求的是服务器,但不会刷新页面. 例如在注册功 ...
- Splay tree
类别:二叉排序树 空间效率:O(n) 时间效率:O(log n)内完成插入.查找.删除操作 创造者:Daniel Sleator和Robert Tarjan 优点:每次查询会调整树的结构,使被查询频率 ...
- (CodeForces )540B School Marks 贪心 (中位数)
Little Vova studies programming to p. Vova is very smart and he can write every test for any mark, b ...
- 几个常用方法有效优化ASP.NET的性能
一. 数据库访问性能优化 1),数据库的连接和关闭 访问数据库资源需要创建连接.打开连接和关闭连接几个操作.这些过程需要多次与数据库交换信息以通过身份验证,比较耗费服务器资源.ASP.NET中提供了连 ...
- SPA架构
databus ajax处理得到得数据 service 对databus做缓存,以及业务(如评论列表,评论详情) component 组件 html+css+js组成 evenbus 组件之间通讯,数 ...