# C++ STL Set 快速入门

Set

set经常用来保存一组数据，他们获得和使用的顺序无关紧要，只需要考虑是否是在集合中即可。

```    #include <iostream>
#include <iterator>
#include <algorithm>
#include <set>
using namespace std;
struct classcomp
{
bool operator()(const char&amp; lhs,const char&amp; rhs)
{
return lhs > rhs;
}
};
char array[] = {'e','f','g'};
int _tmain(int argc, _TCHAR* argv[])
{
set<char,classcomp> myset;
myset.insert('a');
myset.insert('b');
myset.insert('c');
copy(myset.begin(),myset.end(),ostream_iterator<char>(cout," "));
cout<<endl;
cout<<myset.insert('d').second<<endl;
//Now a b c d

myset.insert(array,array+3);
copy(myset.begin(),myset.end(),ostream_iterator<char>(cout," "));
cout<<endl;
//Now g f e d c b a
myset.erase('d');
//Now g f e c b a
myset.erase(myset.begin());
//Now f e c b a
copy(myset.begin(),myset.end(),ostream_iterator<char>(cout," "));
cout<<endl;
//myset.clear();//清空set
set<char,classcomp>::iterator it= myset.begin();
if ((it = myset.find('k')) == myset.end())
{
cout<<"Can not find K in this set"<<endl;
}
cout<<"Current size is :"<<myset.size()<<endl;
}
```

0人收藏

0

0

1. yuer 发表 2018-07-27 08:46:07 coredump之百米之内必有解药
2. hev 发表 2018-04-28 06:11:38 一个简单、轻量的 Linux 协程实现
3. hev 发表 2017-10-19 15:56:11 FSH – 助你接入私有网络中的 Linux 终端
4. gonwan 发表 2015-04-15 08:03:07 Database Access Layer in C++
5. gonwan 发表 2015-12-28 08:41:13 Basic Usage of Boost MultiIndex Containers
6. gonwan 发表 2016-01-19 03:37:54 Coroutines in C++/Boost
7. Haoxiang Li 发表 2017-10-25 20:29:02 MXNet C++ Deployment
8. yuer 发表 2017-10-20 07:52:47 基于leveldb的持久消息队列SDK
9. yuer 发表 2017-10-07 07:51:32 c++11完美转发
10. 博主 发表 2016-09-03 00:00:00 C++编译期类型信息的利用
11. yuer 发表 2017-09-06 03:03:29 libcurl访问unix socket
12. yuer 发表 2017-09-07 08:14:58 valgrind检测php扩展的warning