c ++ stl学习笔记2

目录

set

pair 

map

unordered_map、unordered_set 、bitset


set

set库包含两个:set 和multiset

set 是有序集合 ,multiset 是有序多重集合。

头文件:

# inlcude<set>

声明方法:

set<int> s ;
set<int> s1 = {2,3,1} ; // 不管怎么初始化,set都会默认从小到大排成有序的序列
set<int,greater<int>> s2 = {3,1,5,2} ; // 加上greater<>后就会把它变成从大到小排序
set<double> s3 ;
multiset<set> s4 ;

 操作:
1. .size()、.empty()、.clear()

和vector容器是类似的

 2.迭代器 .begin() .end()

vector的时候就再说迭代器,就把它理解为指针即可,可以使用*解除引用,支持++ 、--操作

 一定要注意,所有的.end()方法(不仅是set的.end(),这里说的是所有容器的.end())都是返回的是序列最后一个元素的后一个位置,即是越界的!

set<int>::iterator it ; // 迭代器可以支持*解除引用取元素值,支持 ++ 、-- 
for(it = s.begin() ; it != s.end() ; it ++)
{
	cout << *it << " " ;
}

3. .insert(x)、.find(x)

.insert(x)将x插入到有序set中

.find(x)查找x,若找到,就返回该元素的迭代器,若找不到就返回s.end() (这里*s.end()是一个不确定的值,因为s.end()已经越界了,相当于s[s.size()] )

4. .erase(x)

删除元素x 

5. .count(x)

返回x的个数

6. .lower_bound()、upper_bound()

s.lower_bound(x)查找大于等于x的元素中最小的一个,并返回指向该元素的迭代器

s.upper_bound(x)查找大于x的元素中最小的一个,并返回指向该元素的迭代器

试一试:

# include<iostream>
# include<set>

using namespace std ;

int main()
{
	// 包含set 和 multiset一个是有序集合,另一个是有序多重集合(可以有重复)
	set<int> s = {1,2,3} ;
	set<int ,greater<int>> s2 = {1,5,2,4,3} ; // 从大到小排序 
	set<double> sd ;
	multiset<int> ms ;
	// size empty clear 都可以
	cout << "s.size() : " << s.size() << endl ;
	cout << "s.empty() : " << s.empty() << endl ;
	s.clear() ;
	cout << "s.size() : " << s.size() << endl ;
	cout << "s.empty() : " << s.empty() << endl ;
	s = {4,5,2,6} ;
	// 迭代器 .insert(x) .find(x) .begin() .end() 
	s.insert(3) ;
	cout << "s.size() : " << s.size() << endl ;
	
	set<int>::iterator it ; // 迭代器可以支持*解除引用取元素值,支持 ++ 、-- 
	for(it = s.begin() ; it != s.end() ; it ++)
	{
		cout << *it << " " ;
	}
	cout << endl ;
	for(it = s2.begin() ; it != s2.end() ; it++)
	{
		cout << *it << " " ;
	}
	cout << endl ;
	cout << "*s.find(1):" << *s.find(1) << endl ; // 找到就返回指向该元素的迭代器,没有找到就返回s.end() 相当于s[s.size()],注意,是越界的!! 
	cout << "*s.find(4):" << *s.find(4) << endl ;
	// .erase(x) 删除元素x
	s.erase(2) ;
	for(it = s.begin() ; it != s.end() ; it ++)
	{
		cout << *it << " " ;
	}
	cout << endl ;
	 
	cout << "count:" << s.count(4) ;
	 
	return 0 ;
} 

pair 

头文件
# include<utility>

声明:

pair<int,int> p ;
pair<int,string> p2 ;
p = {1,2} ;

操作: 

.first()和.second()取两个元素

 pair和vector都支持比较,a ==b a!=b a>b a<b 等等按照字典序来比较

map

非常nb

和python中的字典类似

可以将key和value映射起来

头文件:

# incldue<map>

声明:

总之就是:map<key_type,value_type> name ;
map<double,int> h ;
map<string,int> h2 ;
map<pair<int,int>,string> h3 ;

 操作:

1. .size() .empty() .clear()

2. .insert({key,value}) .erase(key)

3. [] 操作符

[] 操作,像数组一样可以去取key来得到value,还可以通过h[key]来进行赋值,时间效率是O(logn)。

试一试:

# include<iostream>
# include<map>

using namespace std ;

int main()
{
	map<string,int> rank ;
	map<pair<int,int>,int> h ;
	
	// .insert({}) .erase({}) .size() .empty() .clear
	rank.insert({"rds",1}) ;
	h.insert({{1,1},2}) ;
	
	
	cout << rank.find("rds")->second << endl  ;
	cout << rank["rds"] << endl ;
	
	// .find(key) 查找有没有key为key的元素 
	cout << h.find({1,1})->second<< endl ;
	// [] 操作,像数组一样可以去取下标,只不过时间效率是O(logn) 
	cout << h[{1,1}] << endl ;
	rank.erase("rds") ;
	cout << rank.size() ;
	return 0 ; 
}

unordered_map、unordered_set 、bitset

头文件

# include<unorder_map> map是无序的,但是时间效率是O(1)

# include<unorder_set> set是无序的,不支持二分 时间效率是O(1)

# include<bitset> 这个是提供了一个容纳二进制串的容器

声明

bitset<1000>a , b ;ab两个二进制串都是1000

操作: 

a.set(3)把a[3]设置成1

a.reset(3)把a[3]设置成0

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

rds.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值