vector & set & map & unordered_set & unordered_set

背景:在此之前,自己对以上五个概念从未接触过,最近在刷LeetCode算法题,发现对这5个概念的考察频率极高,由此可见其重要性。在此仅做基础了解。


1. Vector

容器类型有多种,这里的vector属于序列容器。有以下特性:

(1)自动内存管理。可以动态地改变vector对象的长度,并随着元素的增加和删除而增大和缩小;(vector可看作是特殊的数组,它们的物理地址都连续,对array的处理方法也适用于vector,但数组array的大小固定,而vector的size可改变)

(2)支持对元素的随机访问

(3)也是可翻转容器(可从尾部向首部访问元素)。

2. map & set 属于关联容器,unordered_map & unordered_set 是C++11新增的。

使用vector、map、set、unordered_map 、 unordered_set前必须先包含相应的头文件。

#include<vector>
#include<set>
#include<unordered_set>
#include<map>
#include<unordered_map>

在LeetCode的练习中,经常是将map,set,unordered_map,unordered_set添加到vector中,即vector能够存放各种类型的对象

#include<vector>
#include<set>
#include<unordered_set>
#include<map>
#include<unordered_map>
#include<iostream>

using namespace std;

int main()
{
	//declare 
	vector<int> a(3);
	vector<set<int>> b(3);
	vector<unordered_set<int>> c(3);
	vector<map<int, int>> d(2);
	vector<unordered_map<int, int>> e(2);

	//initializition
	a = { 1,2,3 };
	b = { {4},{5},{6}};
	c = { {55},{66},{77},{88} };
	
	d[0][0] = 10;
	d[1][1] = 9;

	e[0][0] = 10;
	e[1][1] = 9;

}

2.3 通过阅读C++ 中vector的英文手册学习vector:http://www.cplusplus.com/reference/vector/vector/ 

vector作为C++提供的一个类,其支持的函数差不多有30个,除常见的几个外,别的可通过查阅手册使用。

2.4 通过阅读C++ 中set的英文手册学习set:http://www.cplusplus.com/reference/vector/vector/

Sets are containers that store unique elements following a specific order.

set即集合的意思,同python中的set一样,C++中set中的元素也是不重复的,而且以一种特定的顺序规则存储。unordered_set的区别仅仅是其中存储的元素是无序的,不以特定的顺序存储。

set作为C++提供的一个类,其支持的成员函数较多,查阅手册使用。

2.5 通过阅读C++ 中map的英文手册学习map:http://www.cplusplus.com/reference/map/map/

类似python中的字典,C++ map中的元素也是通过key-value对的方式存储的,且元素通过key的特定顺序排列。unordered_map的区别仅仅是其中存储的元素是无序的,不根据key的特定顺序存储。

3 简单实践

3.1 vector      https://www.runoob.com/w3cnote/cpp-vector-container-analysis.html

3.1 set   https://www.cnblogs.com/caiyishuai/p/8646345.html

3.1 map   Leetcode T36

 

 

 

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值