C++STL之Set集合容器
来源:互联网 发布:mac炒股软件 知乎 编辑:程序博客网 时间:2024/05/16 06:26
Set集合容器实现了红黑树的平衡二叉树的数据结构,在插入元素时候,它会自动的进行调整二叉树的排列,把该元素放到适当的位置,以保证每个子树根节点的键值大于左子树所有的键值,小于右子树所有节点的键值;另外,还要确保根节点左子树的高度与右子树的高度相等,这样,二叉树的高度最小,从而达到检索的速度最快
注意:不会插入相同的键值,若存在相同的键值,第二次插入忽略。
平衡二叉检索树的检索使用的是二叉树的中序遍历算法,检索效率高于vector,deque,和list等容器。另外,采用中序遍历算法可将键值由小到大遍历出来,所以,可以这么去理解,平衡二叉树在插入元素时,就会自动的将元素按键值从小到大顺序排列。
对于set容器中的键值,不可直接修改。因此如果把容器中的一个键值修改了,set容器就会根据新的键值旋转子树,以保持新的平衡,这样,修改的键值很可能就不在原来的那个位置上了,可以这么说,构造set集合主要目的就是为了快速检索。
multiset,map,multimap的内部结构也是平衡二叉树,但是这4个之间到底有什么不同呢??一个个来看
*元素的插入与中序遍历
采用insert()方法把元素插入集合中去,插入的具体规则在默认的比较规则下,是按元素的由小到大插入语,如果指定了比较规则函数,则按自定义的比较规则函数插入。
使用前向迭代器对集合进行中序遍历,其结果正好是元素的排序结果
*反向遍历
使用反向迭代器reverse_iterator可以反向遍历。注意使用rbegin(), rend()
#include<iostream>#include<set>using namespace std;int main(){//定义元素类型为int的集合对象s,当前没有任何元素set<int> s;s.insert(8);s.insert(1);s.insert(12);s.insert(6);s.insert(8);//第二次插入元素8//正向输出 set<int>::iterator it;for(it = s.begin(); it != s.end(); it ++)cout<<*it<<" ";cout<<endl;//逆向输出set<int>::reverse_iterator rit;for(rit = s.rbegin(); rit != s.rend(); rit ++)cout<<*rit<<" ";cout<<endl; return 0; }
*元素的删除
与插入元素的处理方法类似,集合具有高效的删除处理功能,并自动更新调整以保证二叉树的平衡。
删除的元素可以是某个迭代器位置上的元素,等于某键值的元素,一个区间上的元素或者是清空集合
#include<iostream>#include<set>using namespace std;int main(){set<int> s;s.insert(8);s.insert(1);s.insert(12);s.insert(6);s.insert(8);//第二次插入元素8//删除键值为6的那个元素 s.erase(6);set<int>::reverse_iterator rit;for(rit = s.rbegin(); rit != s.rend(); rit ++)cout<<*rit<<" ";cout<<endl;//删除第一个元素,即删除了元素1 s.erase(s.begin());for(rit = s.rbegin(); rit != s.rend(); rit ++)cout<<*rit<<" ";cout<<endl;//清空集合s.clear();cout<<s.size()<<endl; return 0;}
使用find()方法对集合元素进行搜索,如果找到要查找的键值,则返回该键值所在的迭代器位置,否则,返回集合最后一个元素后的一个位置,即返回了end()
#include<iostream>#include<set>using namespace std;int main(){set<int> s;s.insert(8);s.insert(1);s.insert(12);s.insert(6);s.insert(8);//第二次插入元素8set<int>::iterator it;it = s.find(6);if(it != s.end())cout<<*it<<endl;elsecout<<"Not find it!"<<endl;it = s.find(20);if(it != s.end())cout<<*it<<endl;elsecout<<"Not find it!"<<endl;return 0;}
*自定义比较函数
使用insert()将元素插入到set集合中去的时候,集合会根据设定的比较函数将该元素放到应该放到的节点上去,在定义集合的时候,如果没有指定比较函数,那么采用默认的比较函数,即按键值由小到大的顺序插入元素,很多情况下,是需要你自己编写比较函数的。
1. 如果元素不是结构体,那么,可以编写比较函数,下面的比较函数是按照从大到小的插入到集合中的
#include<iostream>#include<set>using namespace std;//自定义比较函数mycmp,重载() 操作符struct mycmp{bool operator () (const int& a, const int& b){if(a != b)return a > b;elsereturn a > b;}};int main(){set<int, mycmp> s;s.insert(8);s.insert(1);s.insert(12);s.insert(6);s.insert(8);//第二次插入元素8set<int, mycmp>::iterator it;for(it = s.begin(); it != s.end(); it ++)cout<<*it<<" ";cout<<endl;return 0;}
2. 如果元素是结构体的话,那么,可以把比较函数写到结构体中
#include<iostream>#include<set>#include<string>using namespace std;struct Info{string name;float score;//重载< 操作符,自定义排序规则bool operator < (const Info& a) const{return a.score < score;}};int main(){set<Info> s;Info info;info.name = "CaiCai";info.score = 80.5;s.insert(info);info.name = "Ningbo";info.score = 20.5;s.insert(info);info.name = "Zhejiang";info.score = 60.5;s.insert(info);set<Info>::iterator it;for(it = s.begin(); it != s.end(); it ++)cout<<(*it).name<<":"<<(*it).score<<endl;return 0;}
set集合就介绍这么多,大家在遇到这样的问题,可以用set解决。另外想想,如果想插入重复的键值怎么办?
- C++STL之Set集合容器
- c++STL Set集合容器
- STL之set集合容器
- STL之set集合容器
- STL之set集合容器
- STL之set集合容器
- STL之set集合容器
- STL之set集合容器
- STL之set集合容器
- 【C++】STL常用容器总结之九:集合set
- C++STL之set容器
- stl之set集合容器应用基础
- STL关联式容器之集合set
- STL set集合容器
- C++STL之关联容器【map】【set】
- C++--STL---set容器
- c++STL通用容器 之 集合
- C++STL之multiset多重集合容器
- 安卓JNI使用C++类
- 数据库基础
- 导出excel 利用xml文件
- Linux 系统挂载数据盘
- PHPCMS V9自定义分页函数
- C++STL之Set集合容器
- android社交第三方应用分享
- 4种java复制文件的方式
- 黑马程序员——Java基础--------内部类
- T-sql语句查询执行顺序
- phpcms栏目页调用栏目图片的最佳解决方法
- Java学习之Iterator(迭代器)的一般用法 (转)
- PHPCMS V9关于栏目图片调用的方法
- 全面理解http