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 ...
随机推荐
- React入门资源整理
另外,附上我搜集的一些比较实用的学习资料,建议先看这些撸起来,再看什么乱七八糟的awsome系列. React入门资源整理 React项目新手指南 http://www.w3ctech.com/top ...
- 一个transaction异常的处理
11-16 14:13:47.715: W/dalvikvm(16771): threadid=1: thread exiting with uncaught exception (group=0x4 ...
- eclipse Ctrl+1 没反应
今天上午写代码,突然发现Ctrl+1没反应了,顿时无语.昨天还好好的,今天就不行了…… 无奈,只好在网上查了查,据说快捷键冲突的原因比较大. 于是我将Ctrl+1换成了Alt+1.在eclipse中测 ...
- c# winform实现网页上用户自动登陆,模拟网站登录
using System; using System.Collections.Generic; using System.Text; using System.Net; using System.IO ...
- 【转】【SQL SERVER】怎样处理作业中的远程服务器错误(42000)
(SQL SERVER)怎样处理作业中的远程服务器错误(42000) 问: 1.我创建了一个链接服务器. 2.在两台服务器之间创建了新的SQL用户. 3.编写了访问链接服务器的SQL语句,执行成功. ...
- WCF学习系列一_创建第一个WCF服务
原创作者:灰灰虫的家http://hi.baidu.com/grayworm WCF开发实战系列一:创建第一个WCF服务 在这个实战中我们将使用DataContract,ServiceContract ...
- MP4(一)-结构
http://blog.csdn.net/zhuweigangzwg/article/details/17222951 一.基本概念 1.mp4概述 MP4文件中的所有数据都装在box(QuickTi ...
- php session_id()函数介绍及代码实例
session_id()功能: 获取设置当前回话ID. 函数说明: string session_id ([ string $id ] ) 参数: 如果指定了参数$id,那么函数会替换当前的回话id. ...
- iOS 查找字符串 相同 子字符串的位置 range
问题:解决替换同一个字符串的多个相同的字符eg. xxx这个超级大土豪白送xxx一个!赶快来抢把! 将第一个xxx换成名字 将第二个xxx换成物品 两种办法 第二种办法更灵活一点 //第一种办法 ...
- html-----014---统一资源定位器
HTML 统一资源定位器 URL 可以由单词组成,比如 “w3school.com.cn”,或者是因特网协议(IP)地址:192.168.1.253.大多数人在网上冲浪时,会键入网址的域名,因为名称比 ...