C++ STL set集合容器常用用法
来源:互联网 发布:北国知春物业 编辑:程序博客网 时间:2024/04/30 02:27
set集合容器:实现了红黑树的平衡二叉检索树的数据结构,插入元素时,它会自动调整二叉树的排列,把元素放到适当的位置,以保证每个子树根节点键值大于左子树所有节点的键值,小于右子树所有节点的键值;另外,还得保证根节点左子树的高度与右子树高度相等。
平衡二叉检索树使用中序遍历算法,检索效率高于vector、deque和list等容器,另外使用中序遍历可将键值按照从小到大遍历出来。
构造set集合主要目的是为了快速检索,不可直接去修改键值。
常用操作:
1.元素插入:insert()
例:
set<int> s;
......
set<int>::reverse_iterator rit;
for(rit=s.rbegin();rit!=s.rend();rit++)
4.元素删除:与插入一样,可以高效的删除,并自动调整使红黑树平衡。
set<int> s;
s.erase(2); //删除键值为2的元素
s.clear();
5.元素检索:find(),若找到,返回该键值迭代器的位置,否则,返回最后一个元素后面一个位置。
set<int> s;
set<int>::iterator it;
it=s.find(5); //查找键值为5的元素
if(it!=s.end()) //找到
cout<<*it<<endl;
else //未找到
cout<<"未找到";
6.自定义比较函数
(1)元素不是结构体:
例:
//自定义比较函数myComp,重载“()”操作符
struct myComp
{
bool operator()(const your_type &a,const your_type &b)
[
return a.data-b.data>0;
}
}
set<int,myComp>s;
......
set<int,myComp>::iterator it;
(2)如果元素是结构体,可以直接将比较函数写在结构体内。
例:
struct Info
{
string name;
float score;
//重载“<”操作符,自定义排序规则
bool operator < (const Info &a) const
{
//按score从大到小排列
return a.score<score;
}
}
set<Info> s;
......
set<Info>::iterator it;
平衡二叉检索树使用中序遍历算法,检索效率高于vector、deque和list等容器,另外使用中序遍历可将键值按照从小到大遍历出来。
构造set集合主要目的是为了快速检索,不可直接去修改键值。
常用操作:
1.元素插入:insert()
2.中序遍历:类似vector遍历(用迭代器)
例:
set<int> s;
......
set<int>::iterator it;
for(it=s.begin(); it!=s.end(); it++)
例:
set<int> s;
......
set<int>::reverse_iterator rit;
for(rit=s.rbegin();rit!=s.rend();rit++)
4.元素删除:与插入一样,可以高效的删除,并自动调整使红黑树平衡。
set<int> s;
s.erase(2); //删除键值为2的元素
s.clear();
5.元素检索:find(),若找到,返回该键值迭代器的位置,否则,返回最后一个元素后面一个位置。
set<int> s;
set<int>::iterator it;
it=s.find(5); //查找键值为5的元素
if(it!=s.end()) //找到
cout<<*it<<endl;
else //未找到
cout<<"未找到";
6.自定义比较函数
(1)元素不是结构体:
例:
//自定义比较函数myComp,重载“()”操作符
struct myComp
{
bool operator()(const your_type &a,const your_type &b)
[
return a.data-b.data>0;
}
}
set<int,myComp>s;
......
set<int,myComp>::iterator it;
(2)如果元素是结构体,可以直接将比较函数写在结构体内。
例:
struct Info
{
string name;
float score;
//重载“<”操作符,自定义排序规则
bool operator < (const Info &a) const
{
//按score从大到小排列
return a.score<score;
}
}
set<Info> s;
......
set<Info>::iterator it;
0 0
- C++ STL set集合容器常用用法
- C++ STL set集合容器常用用法
- C++ STL set集合容器常用用法
- C++ STL set集合容器常用用法
- c++STL Set集合容器
- C++/STL关联容器-集合-multiset&set的用法
- 【C++】STL常用容器总结之九:集合set
- C++ STL set集合容器常见用法
- C++STL之Set集合容器
- C++ STL set容器常用用法
- C++ STL set容器常用用法
- C++ STL set容器常用用法
- C++ STL set容器常用用法
- STL set集合容器
- set集合容器用法
- set集合容器用法
- C++--STL---set容器
- STL之set集合容器
- STL源码——特殊的空间配置器
- linear-gradient 用法小结,不做渐变,做斜型背景。
- CodeIgniter(1)简单入门---使用mvc来完成对新闻的一组操作----增删改查
- i2cdetect i2cdump i2cget i2cset用法
- ES6 解析赋值
- C++ STL set集合容器常用用法
- javascript (function(){})()
- ssh三框架整合
- 安卓webwiew使用
- 服务器宕机记录16.12.14
- 自定义标签
- Android StepView物流进度
- Retrofit -网络请求
- 历史上的今天 —— Google doodle