您的位置:首页 > 其它

STL中set的使用

2017-02-04 22:45 148 查看
#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: