【数据结构】STL——set容器

set为已经排好序的集合,且不允许有相同元素,最适合快速查找操作

使用时需要添加头文件#include <set>

其insert函数原型如下:pair<iterator, bool> insert(const T &val);设返回值为对象x,当插入对象不存在时,则插入成功,x.second = true;当插入对象已经存在时,插入失败,x.second = false;

测试代码如下:

#include <iostream>
#include <set>
using namespace std;
int main()
{
	typedef set<int>::iterator IT;
	int a[5] = {3, 4, 6, 1, 2};
	set<int> st(a, a+5);			//创建set对象
	pair<IT, bool> result;
	result = st.insert(5);

	if(result.second)
		cout<<*result.first<<" inserted"<<endl;
	if(st.insert(5).second)
		cout<<*result.first<<endl;
	else
		cout<<*result.first<<" already exists"<<endl;

	pair<IT, IT> bounds = st.equal_range(4);
	cout<<*bounds.first<<","<<*bounds.second;
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值