set集合:集合中没有重复的元素(输入相同的元素时只保留一个,重复插入无效),并且在默认的情况下对集合里的元素按升序排序。所以涉及到排序并且不能重复时,想一下可不可以用set。
0.size();//返回当前set容器中的元素的个数
1.创建set对象:创建set对象时,需要指定元素的类型
set<int>s;
set<string>s;
2.元素的插入与遍历
采用insert()的方法把元素插入到集合中,
[html] view plain copy
- <span style="font-size:18px;">#include<iostream>
- #include<cstdio>
- #include<cstring>
- #include<string>
- #include<set>
- int main()
- {
- set<int>s;
- set<int>::iterator it;//迭代器
- s.insert(5);//第一次插入5,可以插入
- s.insert(1);
- s.insert(3);
- s.insert(5);//第二次插入5,重复元素,不会插入
- for(it=s.begin();it!=s.end();it++)
- printf("%d ",*it);
- printf("\n");
- }
- //运行结果1 3 5</span>
3.元素的反向遍历
使用反向迭代器reverse_iterator可以反向遍历集合,需要用到rbegin(),rend();
[html] view plain copy
- <span style="font-size:18px;">#include<iostream>
- #include<cstdio>
- #include<cstring>
- #include<string>
- #include<set>
- using namespace std;
- int main()
- {
- set<int>s;
- set<int>::reverse_iterator it;//反向迭代器
- s.insert(5);
- s.insert(1);
- s.insert(3);
- for(it=s.rbegin();it!=s.rend();it++)
- printf("%d ",*it);
- printf("\n");
- }
- //运行结果5 3 1</span>
4.元素的删除
1. erase(iterator) ,删除定位器iterator指向的值
myset.erase(myset.begin());
[html] view plain copy
- <span style="font-size:18px;">#include<iostream>
- #include<cstdio>
- #include<cstring>
- #include<string>
- #include<set>
- using namespace std;
- int main()
- {
- set<int>s;
- set<int>::iterator it;//反向迭代器
- s.insert(5);
- s.insert(1);
- s.insert(3);
- s.insert(7);
- s.erase(1);//删除键值为1的元素
- for(it=s.begin();it!=s.end();it++)
- printf("%d ",*it);
- printf("\n");</span>
[html] view plain copy
- <span style="font-size:18px;"> s.clear();//清空
- }
- //运行结果3 5 7
- </span>
5.元素的检索
使用find()方法对集合进行检索,如果找到查找的的键值,则返回该键值的迭代器位置;否则,返回集合最后一个元素后面的一个位置,即end()。
[html] view plain copy
- #include<iostream>
- #include<cstdio>
- #include<cstring>
- #include<string>
- #include<set>
- using namespace std;
- int main()
- {
- set<int>s;
- set<int>::iterator it;//反向迭代器
- s.insert(5);
- s.insert(1);
- s.insert(3);
- it=s.find(3);
- if(it!=s.end())
- cout<<*it<<endl;
- else
- cout<<"not find it"<<endl;
- it=s.find(7);
- if(it!=s.end())
- cout<<*it<<endl;
- else
- cout<<"not find it"<<endl;
- }
- //运行结果3 not find end
6.自定义比较函数
使用insert将元素插入到集合中去的时候,集合会根据设定的比较函数奖该元素放到该放的节点上去。在定义集合的时候,如果没有指定比较函数,那么采用默认的比较函数,即按键值从小到大的顺序插入元素。但在很多情况下,需要自己编写比较函数。
编写比较函数有两种方法。
(1)如果元素不是结构体,那么可以编写比较函数。下面的程序比较规则为按键值从大到小的顺序插入到集合中。
[html] view plain copy
- #include<iostream>
- #include<set>
- using namespace std;
- struct mycomp
- { //自定义比较函数,重载“()”操作符
- bool operator() (const int &a, const int &b)
- {
- if(a != b)
- return a > b;
- else
- return a > b;
- }
- };
- int main()
- {
- set<int, mycomp> s; //采用比较函数mycomp
- s.insert(5); //第一次插入5,可以插入
- s.insert(1);
- s.insert(6);
- s.insert(3);
- s.insert(5); //第二次插入5,重复元素,不会插入
- set<int,mycomp>::iterator it;
- for(it = s.begin(); it != s.end(); it++)
- cout << *it << " ";
- cout << endl;
- return 0;
- }
- /*
- 运行结果:6 5 3 1
- */
(2)如果元素是结构体,那么可以直接把比较函数写在结构体内。
[html] view plain copy
- #include<iostream>
- #include<set>
- #include<string>
- using namespace std;
- struct Info
- {
- string name;
- double score;
- bool operator < (const Info &a) const // 重载“<”操作符,自定义排序规则
- {
- //按score由大到小排序。如果要由小到大排序,使用“>”即可。
- return a.score < score;
- }
- };
- int main()
- {
- set<Info> s;
- Info info;
- //插入三个元素
- info.name = "Jack";
- info.score = 80;
- s.insert(info);
- info.name = "Tom";
- info.score = 99;
- s.insert(info);
- info.name = "Steaven";
- info.score = 60;
- s.insert(info);
- set<Info>::iterator it;
- for(it = s.begin(); it != s.end(); it++)
- cout << (*it).name << " : " << (*it).score << endl;
- return 0;
- }
- /*
- 运行结果:
- Tom : 99
- Jack : 80
- Steaven : 60 */