STL中set的使用

#include <cmath>
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <set>
#include <iterator>
using namespace std;

int main()
{
	typedef set<double > double_set;//等价于typedef set<double,less<double> > double_set;
	const int SIZE=5;
	double a[SIZE]={2.1, 4.2, 9.5, 2.1, 3.7};
	double_set doubleSet(a,a+SIZE);
	ostream_iterator<double> output(cout," ");
	cout<<"1)";
	copy(doubleSet.begin(),doubleSet.end(),output);
	cout<<endl; 
	
	pair<double_set::const_iterator,bool> p;
	p=doubleSet.insert(9.5);	//insert()函数返回值是一个pair对象,其first是被插入元素的迭代器,
							   //second代表是否成功插入了 
							  //因为插入的是9.5,而9.5 set容器早已含有,所以插入不成功 
	
	if(p.second)
		cout<<"2)"<<*(p.first)<<" inserted"<<endl;
	else
		cout<<"2)"<<*p.first<<" not inserted"<<endl; //*p.first等价于*(p.first) 
	return 0;
} 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值