STL常用函数复习之————set

//	set	multiset	map		multimap的内部结构是平衡树
//	二叉平衡树的检索使用中序遍历算法 
//	对于这些容器中的键值,不可以直接修改
//	set默认从小到大 
#include<bits/stdc++.h>
using namespace std;
set<int> s;
int main(){
	//插入	s.insert(8);	重复元素不会插入
	
	/*遍历	set<int>::iterator it
	 	for(it=s.begin(); it!=s.end(); it++)
	*/
	
	/*反向遍历	set<int>::reverse_iterator rit	定义反向迭代器
		for(rit=s.rbegin(); rit!=s.rend(); rit++)
		rbegin()    返回的值和end()相同
		rend()     返回的值和rbegin()相同
	*/
	
	/*删除	 	s.erase(s.begin());
				s.erase(s.begin()+1,s.end()-1);
				s.erase(8);*/
				
	/*检索		如果找到,返回迭代器的位置;没找到,返回end()
			set<int>::iterator it;
			it=s.find(8);*/ 
			
	/*自定义比较函数
	1.元素不是结构体,重载"()"运算符
		struct myCOMP
		{
			bool operator()(const int &a, const int &b)	//(函数内部的判断是 a<b 看的返回值)
			{
				if(a!=b)	return a>b;	//按照键值从大到小排列
				else	return a>b;
			}
		}
		set<int,myCOMP> s;
	2.元素是结构体,直接把比较函数写在结构体内
		struct inf
		{
			string name;
			float score;
			//重载 < 号,自定义排序规则 
			bool operator < (const inf &a) const
			{
				 return a.score<b.score;	//score由大到小排列;要由小到大,换成>;
			}
		};
		set<inf> s;*/
		
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值