#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;
} STL中set的使用
最新推荐文章于 2025-03-05 09:44:17 发布
本文展示了一个使用C++标准库中的set容器的例子,通过定义一个双精度浮点数的集合,并演示如何插入元素及检查重复插入的情况。该示例还介绍了如何利用ostream_iterator输出容器内的元素。
3万+

被折叠的 条评论
为什么被折叠?



