C++中的集合和字典(unordered_set, unordered_map)
C++中的集合和字典是非常常用的结构,插入/查找都是O(1),代码示例挑选于C++ reference: http://www.cplusplus.com/reference/unordered_set/unordered_set/clear/
unordered_set:
#include <iostream>
#include <string>
#include <array>
#include <unordered_set>
using namespace std;void SetMain()
{std::unordered_set<std::string> myset = { "yellow","green","blue" };std::array<std::string, 2> myarray = { "black","white" };std::string mystring = "red";myset.insert(mystring); // copy insertionmyset.insert(mystring + "dish"); // move insertionmyset.insert(myarray.begin(), myarray.end()); // range insertionmyset.insert({ "purple","orange" }); // initializer list insertionstd::cout << "myset contains:";for (const std::string& x : myset) std::cout << " " << x;std::cout << std::endl;// Possible output:// myset contains : green blue reddish white yellow black red orange purple// findstd::unordered_set<std::string> myset1 = { "red","green","blue" };std::string input;std::cout << "color? ";getline(std::cin, input);std::unordered_set<std::string>::const_iterator got = myset1.find(input);if (got == myset1.end())std::cout << "not found in myset1";elsestd::cout << *got << " is in myset1";std::cout << std::endl;// size(), empty(), clear()}
unordered_map:
#include <iostream>
#include <string>
#include <unordered_map>void MapMain()
{// unordered_map::findstd::unordered_map<std::string, double> mymap = {{ "mom",5.4 },{ "dad",6.1 },{ "bro",5.9 } };std::string input;std::cout << "who? ";getline(std::cin, input);std::unordered_map<std::string, double>::const_iterator got = mymap.find(input);if (got == mymap.end())std::cout << "not found";elsestd::cout << got->first << " is " << got->second;std::cout << std::endl;// unordered_map::insertstd::unordered_map<std::string, double>myrecipe,mypantry = { { "milk",2.0 },{ "flour",1.5 } };std::pair<std::string, double> myshopping("baking powder", 0.3);myrecipe.insert(myshopping); // copy insertionmyrecipe.insert(std::make_pair<std::string, double>("eggs", 6.0)); // move insertionmyrecipe.insert(mypantry.begin(), mypantry.end()); // range insertionmyrecipe.insert({ { "sugar",0.8 },{ "salt",0.1 } }); // initializer list insertionstd::cout << "myrecipe contains:" << std::endl;for (auto& x : myrecipe)std::cout << x.first << ": " << x.second << std::endl;std::cout << std::endl;// Possible output :// myrecipe contains :// salt: 0.1// eggs : 6// sugar : 0.8// baking powder : 0.3// flour : 1.5// milk : 2// size(), empty(), clear()
}
C++中的集合和字典(unordered_set, unordered_map)相关推荐
- CSharp中的集合与字典:不同数据量的内存占用情况
1. CSharp中的集合 集合(Collection)类是专门用于数据存储和检索的类.这些类提供了对栈(stack).队列(queue).列表(list)和哈希表(hash table)的支持.大多 ...
- C++ 中的集合与字典
基本的关联式容器主要有:set.multiset.map 和 multimap,这四种容器可以分为两组:map 和 set. set 可以理解为我们数学中的集合,它可以包含 0 个或多个不重复的数据. ...
- CSharp中集合与字典Contains效率差别
书接上文<CSharp中的集合与字典:不同数据量的内存占用情况> https://blog.csdn.net/a13407142317/article/details/123078962? ...
- python集合与字典区别_Python中的字典与集合
今天我们来讲一讲python中的字典与集合 Dictionary:字典 Set:集合 字典的语法:Dictionary字典(键值对) 语法: dictionary = {key:value,key:v ...
- Python中的数据序列(元组、集合、字典)
Python中的数据序列 一.元组的定义与使用 1.为什么需要元组 思考:如果想要存储多个数据,但是这些数据是不能修改的数据,怎么做? 答:列表?列表可以一次性存储多个数据,但是列表中的数据允许更改. ...
- 概念一: python 中列表 ,数组, 集合,字典;
1. python 基本数据类型 首先python3 中 自带的 有 六个标准的数据类型: Number(数字) String(字符串) Tuple(元组) List(列表) Set(集合) Dict ...
- set集合判断集合中是否有无元素_Python基础学习笔记(五)集合与字典
本节知识大纲: 集合 字典 一.Set 集合的概念 1. set 集合的性质 set 集合是一组无序的且不能重复的集合,打印 set 集合时会自动消除重复的元素项: set 集合用大括号表示: set ...
- Python中序列、列表、元祖(数组)、集合、字典
序列 序列的两个特点: 1.索引操作符,从序列中抓取一个特定项目 2.和切片操作符,获取序列的一个切片,即一部分序列 Python一些内置函数 1.cmp(A, B):比较可为list.tupl ...
- python字典(dict)+常用方法操作+列表、元组、集合、字典的互相转换
python字典(dict)+常用方法操作+列表.元组.集合.字典的互相转换 字典也是 Python 提供的一种常用的数据结构,它用于存放具有映射关系的数据. 为了保存具有映射关系的数据,Python ...
最新文章
- Eclipse问题集锦
- Possible MySQL server UUID duplication for server
- Python Numpy 从文件中读取数据
- JavaScript label语句
- 【Matlab】求解黎卡提 Riccati 方程 李雅普诺夫 Lyapunov 方程
- 笔记3——C++类的一些特性
- Golang的单引号、双引号与反引号
- UVa 10905 孩子们的游戏
- CSS学习笔记-04 a标签-导航练习
- A Simple Math Problem 矩阵打水题
- 拟牛顿法/Quasi-Newton,DFP算法/Davidon-Fletcher-Powell,及BFGS算法/Broyden-Fletcher-Goldfarb-Shanno...
- 匹配滤波器及matlab仿真
- 双歧杆菌基因组序列批量下载、基因组注释、antiSMASH合成基因簇挖掘、核心基因的同源性比较。
- 适用于高速公路的查询软件,数据稳定、免维护,可查询高速路况、路线规划、未来天气等信息
- 【工程光学】几何光学基本定律成像概念
- 用计算机怎样搜wifi网,如何用电脑设置wifi?用电脑设置wifi方法介绍
- 云班课计算机题答案,云班课答案获取
- 基于动态时间规整算法(DTW)的语音识别技术研究-含Matlab代码
- CAT客户端架构设计
- 计算机网络实验路由器配置
热门文章
- 8位数码管矩阵键盘c语言程序,BASCOM-8051 动态数码管显示与矩阵键盘处理程序例子...
- 计算机基础win7桌面操作,电脑入门(十一)桌面个性化设置
- HTTPClient模拟登陆人人网
- MPR VTK 三维重建(二)multi-planner reformation 定位线 十字线
- 特征图注意力_CBAM:卷积块注意力模块
- 关于SurfaceHolder.addCallback方法无法调用surfaceCreated 方法不回调的一个建议
- 北风:贷款买房到底应不应该(亲身经历)
- Android中apk瘦身
- python爬虫人人网登陆
- 【无标题】nginx 502 解决 No connection could be made because the target machine actively refused it