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 ...
随机推荐
- vue2总结
1.ref可跨页面取.vue文件的所有内容: <!--echart图自定义公用模板--> <echar-tem ref="echar"> </echa ...
- socket 发送图片
using System;using System.Collections.Generic;using System.Text;using System.Net.Sockets;using Syste ...
- H264编码 封装成MP4格式 视频流 RTP封包
H264编码 封装成MP4格式 视频流 RTP封包 分类: 多媒体编程 2013-02-20 21:31 3067人阅读 ...
- 搭建智能合约开发环境Remix IDE及使用
目前开发智能的IDE, 首推还是Remix, 而Remix官网, 总是由于各种各样的(网络)原因无法使用,本文就来介绍一下如何在本地搭建智能合约开发环境remix-ide并介绍Remix的使用. 写在 ...
- hdu 1542 Atlantis (线段树扫描线)
大意: 求矩形面积并. 枚举$x$坐标, 线段树维护$[y_1,y_2]$内的边是否被覆盖, 线段树维护边时需要将每条边挂在左端点上. #include <iostream> #inclu ...
- Plus One leetcode java
问题描述: Given a non-negative number represented as an array of digits, plus one to the number. The dig ...
- mac使用迁移助理迁移数据之后homestead无法打开
1. 错误大致如下: here was an error while executing `VBoxManage`, a CLI used by Vagrant for controlling Vir ...
- 5月21 回话控制SESSION COOKIE
百科 HTTP普及(无状态性) 超文本传输协议(HTTP,HyperText Transfer Protocol)是互联网上应用最为广泛的一种网络协议.所有的WWW文件都必须遵守这个标准.设计HTTP ...
- link标签实现给网页标题前加一个小图标favicon.ico
使用方法如下:1.<link rel="shortcut icon " type="images/x-icon" href="./favicon ...
- mysql使用sql语句根据经纬度计算距离排序
CREATE TABLE `locationpoint` ( `id` int(11) NOT NULL, `province` varchar(20) NOT NULL, `city` varcha ...