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 ...
随机推荐
- C# 递归缩小图片
需求:图片太大,上传到服务器会非常占用服务器空间,而系统又不要求高清图片,于是就通过递归的方式让图片每次减少10%的大小,当图片大小小于100k的时候就保存在本地,核心代码如下: class Prog ...
- SpringBoot集成TkMybatis插件 (二)
一.Tkmybatis的好处 Tkmybatis是在mybatis框架的基础上提供了很多工具,让开发更加高效.这个插件里面封装好了我们需要用到的很多sql语句,不过这个插件是通过我们去调用它封装的各种 ...
- linux重启后进入了救援模式,无法远程登录
卸载删除了某个文件系统分区的逻辑卷,但是没有修改fstab文件,导致系统重启异常,此时已经无法远程连接了,只能本地登录到救援模式修改
- p1472 Cow Pedigrees
用dp[i][j]记录i个点,组成深度恰好为j的方案数.arr[i][j]记录i个点,深度<=j的方案数.那么dp[i][j]只有i为奇数时不为0.而arr[i][j]等于dp[i][j]的前缀 ...
- p1211 Prime Cryptarithm
直接深搜+检验. #include <iostream> #include <cstdio> #include <cmath> #include <algor ...
- canvas学习之粒子动画
项目地址:http://pan.baidu.com/s/1ccTptc 粒子动画意思就是把一个图片粒子画,然后使用粒子作出动画效果,主要两个问题:一个图片如何粒子化,这里面我们使用canvas的get ...
- php url处理
http_build_query() $data = array("name"=>"callback" , "value"=>& ...
- python记录_day33 线程
##进程就像加工厂,线程是里边的流水线##进程是资源单位,线程是运行单位,每个进程至少有一个线程 即进程是资源分配的最小单位,线程是CPU调度的最小单位 一.线程的创建两种方式,和进程类似1.t = ...
- CF-517C-思维/math
http://codeforces.com/contest/1072/problem/C 题目大意是给出两个数a,b ,找出若干个数p,使得 SUM{p}<=a ,找出若干个数q使得SUM{q} ...
- git拉取远程分支并创建本地分支
本地分支推送至远程 git checkout local_branch git push origin local_branch:remote_branch 一.查看远程分支 使用如下Git命令查看所 ...