set常用函数及其讲解
- 构造set集合的主要目的是为了快速检索,使用set前,需要在程序头文件中包含声明“#include<set>”。
- set集合容器实现了红黑树(Red-Black Tree)的平衡二叉检索树的的数据结构,在插入元素时,它会自动调整二叉树的排列,把该元素放到适当的位置.例如会将数字进行从小到大的默认排序,将string按字典顺序自动排列。
- 创建集合对象。 创建set对象时,需要指定元素的类型,这一点和其他容器一样.
- 采用inset()方法把元素插入到集合中(set像集合中容器中放入元素只有inset()),插入规则在默认的比较规则下,是按元素值从小到大插入,如果自己指定了比较规则函数,则按自定义比较规则函数插入。使用前向迭代器对集合中序遍历,结果正好是元素排序后的结果。
- 元素的方向遍历。 使用反向迭代器reverse_iterator可以反向遍历集合,输出的结果正好是集合元素的反向排序结果。它需要用到rbegin()和rend()两个方法,它们分别给出了反向遍历的开始位置和结束位置。
#include<iostream> #include<set> using namespace std; int main() {set<int> s;s.insert(5); //第一次插入5,可以插入s.insert(1);s.insert(6);s.insert(3);s.insert(5); //第二次插入5,重复元素,不会插入set<int>::iterator it; //定义前向迭代器//中序遍历集合中的所有元素for(it = s.begin(); it != s.end(); it++){cout << *it << " ";}cout << endl;return 0; } //运行结果:1 3 5 6
-
元素的删除
与插入元素的处理一样,集合具有高效的删除处理功能,并自动重新调整内部的红黑树的平衡(集合内部元素的排序)。删除的对象可以是某个迭代器位置上的元素、等于某键值的元素、一个区间上的元素和清空集合。
#include<iostream> #include<set> using namespace std; int main() {set<int> s;s.insert(5); //第一次插入5,可以插入s.insert(1);s.insert(6);s.insert(3);s.insert(5); //第二次插入5,重复元素,不会插入 集合内部:1 3 5 6s.erase(6); //删除键值为6的元素 集合内部:1 3 5set<int>::reverse_iterator rit; //定义反向迭代器 //反向遍历集合中的所有元素for(rit = s.rbegin(); rit != s.rend(); rit++){cout << *rit << " ";}cout << endl; set<int>::iterator it;it = s.begin();for(int i = 0; i < 2; i++)it = s.erase(it); //将 1 3删除for(it = s.begin(); it != s.end(); it++)cout << *it << " ";cout << endl;s.clear(); //将集合清空cout << s.size() << endl; //输出集合的大小,即元素个数return 0; } /* 运行结果: 5 3 1 5 0 */
-
自定义比较函数。使用insert将元素插入到集合中去的时候,集合会根据设定的比较函数奖该元素放到该放的节点上去。在定义集合的时候,如果没有指定比较函数,那么采用默认的比较函数,即按键值从小到大的顺序插入元素。但在很多情况下,需要自己编写比较函数。
-
编写比较函数有两种方法。
-
(1)如果元素不是结构体,那么可以编写比较函数。下面的程序比较规则为按键值从大到小的顺序插入到集合中。
#include<iostream>
#include<set>
using namespace std;
struct mycomp
{ //自定义比较函数,重载“()”操作符bool operator() (const int &a, const int &b){if(a != b)return a > b;elsereturn a > b;}
};
int main()
{set<int, mycomp> s; //采用比较函数mycomps.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)如果元素是结构体,那么可以直接把比较函数写在结构体内。
-
#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 */
部分内容选自 :https://blog.csdn.net/alibaba_lhl/article/details/81151409