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 ...
随机推荐
- mysql查询练习
mysql> #查询每个栏目最贵的商品 mysql> select goods_id,shop_price,cat_id from (select goods_id,shop_price, ...
- 使用XMl序列化器生成xml文件
生成XML文件 创建几个虚拟的短信对象,存在list中 备份数据通常都是备份至sd卡 使用StringBuffer拼接字符串 把整个xml文件所有节点append到sb对象里 sb.append(&q ...
- JavaScript高级程序设计(二):在HTML中使用JavaScript
一.使用<script>元素 1.<script>元素定义了6个属性: async:可选.表示应该立即下载脚本,但不应该妨碍页面中的其他操作,比如下载其他资源或等待加载其他脚本 ...
- iOS开发——常用字符串string相关方法和处理
(持续更新中……) 1,四舍五入 2,剔除字符 3,拼接字符 4,字符个数和长度 5,字符串的比较 6,字符串的范围 7,字符串转Number类型
- iOS 查找字符串 相同 子字符串的位置 range
问题:解决替换同一个字符串的多个相同的字符eg. xxx这个超级大土豪白送xxx一个!赶快来抢把! 将第一个xxx换成名字 将第二个xxx换成物品 两种办法 第二种办法更灵活一点 //第一种办法 ...
- IOS高级开发 runtime(一)
一. 简介 IOS 开发中灵活使用runtime 会提高我们的程序性能和开发速度.要想使用runtime,首先要引入系统的头文件. <span style="font-size:18p ...
- 使用RequireJS优化Web应用前端
require.js官网:http://requirejs.org/docs/download.html 一篇不错的文章:http://www.csdn.net/article/2012-09-27/ ...
- Codeforces Round #299 (Div. 1)
Problem A: 实际上对于一段数字假设和为k,每次取较大的m个进行t次减一操作,最多减去的是min(m*t,k). 明白了这个结论就可以直接二分答案了. #include <bits/st ...
- 页面mask css
<html> <head> <style type="text/css"> .share_mask { position: fixed; top ...
- mysql中 出现No query specified
Mysql----error:no query specified mysql下抛出错误: error:no query specified 出现此错误是sql不合法原因: 如:select * fr ...