set初学---有序集合

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<set>

using namespace std;

const int N=100010;
int a[N];
int count;
set<int>sets;

/*
begin()        ,返回set容器的第一个元素

end()      ,返回set容器的最后一个元素

clear()          ,删除set容器中的所有的元素

empty()    ,判断set容器是否为空

max_size()   ,返回set容器可能包含的元素最大个数

size()      ,返回当前set容器中的元素个数

rbegin     ,返回的值和end()相同

rend()     ,返回的值和rbegin()相同

cout()         ,返回每个数值出现的次数,只可能是0或者1 
*/

int main()
{
	//insert函数 
	for(int i=5; i>=0; i--)
	{
		sets.insert(i);
		sets.insert(5-i);
	}
	//迭代器遍历,发现按照升序排序,即使0 1 2 3 4 5 
	for(set<int>::iterator it=sets.begin() ; it!=sets.end(); it++)
	{
		cout<<*it<<endl;
	}
	//begin()
	cout<<*sets.begin()<<endl;	
	//end()函数  6,发现压给没有插入6啊 
 	cout<<*sets.end()<<endl;	
	//size()函数  6
	cout<<sets.size()<<endl;
	//upper_bound 返回第一个大于该元素的迭代器   4
	cout<<*sets.upper_bound(3)<<endl; 
	//lower_bound 返回第一个大于或者等于该元素的迭代器     3
	cout<<*sets.lower_bound(3)<<endl; 
	//find(5) 返回一个指向被查找元素的迭代器   5     6
	cout<<*sets.find(5)<<endl; 
	cout<<*sets.find(100)<<endl;
	//count()hansu  0   1
	cout<<sets.count(10)<<endl<<sets.count(5)<<endl; 
}

更多详解请关注https://www.cnblogs.com/caiyishuai/p/8646345.html

猜你喜欢

转载自blog.csdn.net/weixin_42333573/article/details/105992799
今日推荐