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 ...
随机推荐
- jpGraph的应用及基本安装配置 BY 命运
1.---jpGraph其实就是一个图表类库,会让开发者们作图非常方便,只要几行代码就可以勾画出非常炫的图表. 官方下载地址是:http://jpgraph.net/download/ jpGra ...
- ASP.NET MVC 使用Echarts
Echarts官网 页面配置: <!DOCTYPE html> <html> <head> <meta name="viewport" c ...
- uploadify上传控件使用
uploadify是JQuery的一个上传插件,实现的效果非常不错,并且带进度显示,我将给大家演示如何使用uploadify进行图片上传, 1.点我下载http://www.uploadify.com ...
- python的交代一
把自己one note上面的摘抄和自己的节选,全部粘贴到博客了,时间宝贵,要得太多,技术栈要慢慢发展,python先放放了,也不知道什么时候正式捡起来. 先把目前养活自己的android.java.c ...
- ACM——A + B Problem (1)
A + B Problem (1) 时间限制(普通/Java):1000MS/3000MS 运行内存限制:65536KByte总提交:5907 测试通过:151 ...
- XMLHttpRequest cannot load的问题解决方法
在chrome中可以用--allow-file-access-from-files 命令来解决这个问题.右键点击chrome的快捷方式选择属性.在目标一栏中添加--allow-file-acces ...
- rc4加密
function RC4(Expression, Password: string): string; var RB : array[0..255] of Integer; X, Y, Z: long ...
- 获取API返回值
//$return=getApiResult($url); // if ($return==200){ // //..... // } function getApiResult($url){ if( ...
- 时间处理总结(二)oracle
不断总结中................. 1.等于land.djsj=to_date('2016/7/26','yyyy-MM-dd')2.大于等于land.djsj>=to_date('2 ...
- apparmor介绍
AppArmor AppArmor 类似于selinux ,主要的作用是设置某个可执行程序的访问控制权限,主要区别就在于apparmor是以路径(path)为基础,而selinux以i节点(inode ...