非关联容器|hash|unordered_map/multimap,unordered_set/multiset
body, table{font-family: 微软雅黑; font-size: 10pt}
table{border-collapse: collapse; border: solid gray; border-width: 2px 0 2px 0;}
th{border: 1px solid gray; padding: 4px; background-color: #DDD;}
td{border: 1px solid gray; padding: 4px;}
tr:nth-child(2n){background-color: #f8f8f8;}
unordered_map & unordered_multimap
|
#include<iostream>
#include<unordered_map>
#include<string>
#include<iterator>
using namespace std;
int main()
{
pair<int,string> arr[5]={
pair<int,string>(1,"北京"),
pair<int,string>(2,"上海"),
pair<int,string>(2,"昆明"),
pair<int,string>(4,"重庆"),
pair<int,string>(3,"天津")
};
unordered_map<int ,string> test1(arr,arr+5);
unordered_map<int ,string>::iterator it = test1.begin();
for(;it!=test1.end();++it)
{
cout<<it->first<<" "<<it->second<<endl;
}
//无序map,不能使用less<>和greater<>来排序了
cout<<"test unordered_multimap:"<<endl;
unordered_multimap<int ,string> test2(arr,arr+5);
for(auto& elem:test2)
{
cout<<elem.first<<" "<<elem.second<<endl;
}
cout<<"test random access:"<<endl;
cout<<test1[1]<<endl; // unordered_multimap不支持随机访问
unordered_multimap<int,string>::iterator umit = test2.find(2);
cout<<umit->first<<" "<<umit->second<<endl;
}
|
|
#include<iostream>
#include<unordered_set>
using namespace std;
int main()
{
int arr[7] = {2,2,1,3,4,5,3};
unordered_set<int> test1(arr,arr+7);
for(auto& elem:test1)
cout<<elem<<" ";
cout<<endl;
cout<<"test unordered_multiset"<<endl;
unordered_multiset<int> test2(arr,arr+7);
for(auto& elem:test2)
cout<<elem<<" ";
cout<<endl;
unordered_multiset<int> test3(arr,arr+7);
cout<<"test random access"<<endl;
cout<<*(test2.find(3))<<endl;
cout<<*(test2.find(2))<<endl;
cout<<"value = 2 cnt:"<<test2.count(2)<<endl;
return 0;
}
|
非关联容器|hash|unordered_map/multimap,unordered_set/multiset的更多相关文章
- 关联容器:unordered_map详细介绍(附可运行代码)
介绍 1 特性 2 Hashtable和bucket 模版 1 迭代器 功能函数 1 构造函数 12示例代码 2 容量操作 21 size 22 empty 3 元素操作 31 find 32 ins ...
- 关联容器:unordered_map详细介绍
版权声明:博主辛辛苦苦码的字哦~转载注明一下啦~ https://blog.csdn.net/hk2291976/article/details/51037095 介绍 1 特性 2 Hashtabl ...
- unordered_map/unordered_set & unordered_multimap/unordered_multiset非关联容器
body, table{font-family: 微软雅黑; font-size: 10pt} table{border-collapse: collapse; border: solid gray; ...
- 09--STL关联容器(map/multimap)
一:map/multimap的简介 map是标准的关联式容器,一个map是一个键值对序列,即(key,value)对.它提供基于key的快速检索能力. map中key值是唯一的.集合中的元素按一定的顺 ...
- 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 Chines ...
- STL的基本使用之关联容器:map和multiMap的基本使用
STL的基本使用之关联容器:map和multiMap的基本使用 简介 map 和 multimap 内部也都是使用红黑树来实现,他们存储的是键值对,并且会自动将元素的key进行排序.两者不同在于map ...
- STL 笔记(二) 关联容器 map、set、multimap 和 multimap
STL 关联容器简单介绍 关联容器即 key-value 键值对容器,依靠 key 来存储和读取元素. 在 STL 中,有四种关联容器,各自是: map 键值对 key-value 存储,key 不可 ...
- 使用multimap创建重复键关联容器
在“使用 <map> 库创建关联容器”一文中,我们讨论了标准库中的 map 关联容器.但那只是 map 容器的一部分.标准库还定义了一个 multimap 容器,它与 map 类似,所不同 ...
- STL的基本使用之关联容器:set和multiSet的基本使用
STL的基本使用之关联容器:set和multiSet的基本使用 简介 set 和 multiSet 内部都是使用红黑树来实现,会自动将元素进行排序.两者不同在于set 不允许重复,而multiSet ...
随机推荐
- linux 的nohup & 和daemon 总结(转)
add by zhj:守护进程貌似跟nohup + &方式启动的进程差不多.都可以实现与终端的无关联. 原文:http://blog.csdn.net/lovemdx/article/de ...
- linux安装Navicat,界面出现乱码解决方法
下载Navicat:navicat112_mariadb_cs_x64.tar.gz 点击" ./start_navicat"安装出现界面便面为乱码 解决办法:打开start_na ...
- 系统管理命令之last
Linux系统中使用以下命令来查看文件的内容: cat 由第一行开始显示文件内容 tac 从最后一行开始显示,可以看出 tac 是 cat 的倒著写! nl 显示的时候,顺道输出行号! mor ...
- [ 翻译]ruby rails相关的常见服务器
原文:http://stackoverflow.com/questions/4113299/ruby-on-rails-server-options 一,Apache vs Nginx ...
- rails常用gem
一,开发模式下 1,better_errors 使用全新的页面替换 Rails 默认的错误页面,显示更多的上下文信息,例如源码 和变量的值:配合binding_of_caller可以执行代码查看变量的 ...
- 52. N-Queens II(数个数)
The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens ...
- SQL 处理排序空值
①oracle默认排序空值在后面 如果想要排序空值在前面可用关键字 NULLS FIRST, 排序空值在后面也有关键字NULLS LAST ②sqlserver默认排序空值在前面 如果想要排序时空值在 ...
- Apache HttpClient4使用教程
基于HttpClient 4.5.2 执行GET请求 CloseableHttpClient httpClient = HttpClients.custom() .build(); Closeable ...
- 20145221 《Java程序设计》第五周学习总结
20145221 <Java程序设计>第五周学习总结 教材学习内容总结 第八章部分 - 异常处理 语法与继承架构 使用try...catch 首先要明确一点:Java中所有错误都会打包为对 ...
- CONFLICT (modify delete)冲突修复
Demo git:(test) git merge feature CONFLICT (modify/delete): path/to/path/config.inc.php deleted in H ...