unordered_map/unordered_set & unordered_multimap/unordered_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;
}
|
unordered_map/unordered_set & unordered_multimap/unordered_multiset非关联容器的更多相关文章
- 非关联容器|hash|unordered_map/multimap,unordered_set/multiset
body, table{font-family: 微软雅黑; font-size: 10pt} table{border-collapse: collapse; border: solid gray; ...
- [C++ Primer] : 第11章: 关联容器
目录 使用关联容器 关联容器概述 关联容器操作 无序容器 使用关联容器 关联容器与顺序容器有着根本的不同: 关联容器中的元素是按关键字来保存和访问的, 按顺序容器中的元素是按它们在容器中的位置来顺序保 ...
- Chapter11(关联容器)--C++Prime笔记
1.关联容器: map关键字-值对,经常被称为关联数组 set中每个元素只有一个关键字,即只保存关键字的容器 ①允许重复的关键字的容器名字都包含multi. ②不保持关键字顺序存储的容器的名字都以但粗 ...
- STL关联容器总结
有序的都不带unordered,即如下: set multiset map multimap 其中带multi的表示关键字可以重复 无序的带unordered,如下: unordered_map un ...
- 关联容器: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 ...
- C++ primer 11章关联容器
map set multimap (关键字可重复出现) multiset 无序 unordered_map (用哈希函数组织的map) unordered_set unordered_multima ...
- c++ 标准库的各种容器(vector,deque,map,set,unordered_map,unordered_set,list)的性能考虑
转自:http://blog.csdn.net/truexf/article/details/17303263 一.vector vector采用一段连续的内存来存储其元素,向vector添加元素的时 ...
- C++基础之关联容器
关联容器 关联容器和顺序容器的本质区别:关联容器是通过键存取和读取元素.顺序容器通过元素在容器中的位置顺序存储和访问元素.因此,关联容器不提供front.push_front.pop_front.ba ...
随机推荐
- Servlet 小知识
1.In servlet 3.0 we have new feature annotations to replace XML.也就是说应该尽量使用annotations Servlet is an ...
- 11月27日 招聘网第七部分,.gitignore的用法。
回想Rails --Active Record Query Interface--Scopes的基本用法: 1.Passing in arguments.例子:scope :name , -> ...
- 微信小程序页面内转发 按钮 转发
通过给 button 组件设置属性 open-type="share",可以在用户点击按钮后触发 Page.onShareAppMessage() 事件,如果当前页面没有定义此事件 ...
- 『TensorFlow』迁移学习
完全版见github:TransforLearning 零.迁移学习 将一个领域的已经成熟的知识应用到其他的场景中称为迁移学习.用神经网络的角度来表述,就是一层层网络中每个节点的权重从一个训练好的网络 ...
- python迭代器概念与应用
结论 )可迭代对象包含迭代器.2)如果一个对象拥有__iter__方法,其是可迭代对象:如果一个对象拥有next方法,其是迭代器.3)生成器是一种特殊的迭代器,生成器自动实现了“迭代器协议”(即__i ...
- 【IDEA】【6】Maven打包
1,打包成jar包 右侧工具栏Maven Projects->项目名称->Lifecycle->package 2,打包时去掉test 右侧工具栏Maven Projects,打开后 ...
- PY3 多组输入
在c语言你能使用while(scanf(“%d”,x) !=EOF)判断输入是否碰到文件结束符(EOF). 但是在python你不能使用while((x=input())!=EOF). 这有两种方法可 ...
- input上传限定文件类型
input上传限定文件类型 accept="image/*" 限定为只能上传图片 accept=”audio/* 限定为只能上传音频 accept=”video/*” 限定 ...
- Oracle查询前几条数据的方法
在Oracle中实现select top N:由于Oracle不支持select top 语句,所以在Oracle中经常是用order by 跟rownum的组合来实现select top n的查询. ...
- git status 查看当前修改文件
可以查看当前已经修改的文件.