set为已经排好序的集合,且不允许有相同元素,最适合快速查找操作
使用时需要添加头文件#include <set>
其insert函数原型如下:pair<iterator, bool> insert(const T &val);设返回值为对象x,当插入对象不存在时,则插入成功,x.second = true;当插入对象已经存在时,插入失败,x.second = false;
测试代码如下:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22#include <iostream> #include <set> using namespace std; int main() { typedef set<int>::iterator IT; int a[5] = {3, 4, 6, 1, 2}; set<int> st(a, a+5); //创建set对象 pair<IT, bool> result; result = st.insert(5); if(result.second) cout<<*result.first<<" inserted"<<endl; if(st.insert(5).second) cout<<*result.first<<endl; else cout<<*result.first<<" already exists"<<endl; pair<IT, IT> bounds = st.equal_range(4); cout<<*bounds.first<<","<<*bounds.second; return 0; }
最后
以上就是畅快菠萝最近收集整理的关于【数据结构】STL——set容器的全部内容,更多相关【数据结构】STL——set容器内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复