c++ stl set 有序性
来源:互联网 发布:多文件上传java 编辑:程序博客网 时间:2024/06/04 18:39
stl 中的set 是有序容器,可以通过传入自定义比较器函数对象的方式,设定想要使用的比较方法。
使用迭代器遍历set的时候,遍历的顺序就是set 中比较器定义的顺序。
set<int> s;// 插入的时候按照从大到小的顺序插入for (int i = 10; i > 0; i--){ s.insert(i);}set<int>::iterator it;// 遍历的时候的输出是从小到大for (it = s.begin(); it != s.end(); ++it){ cout << *it ;}
可以通过传入比较器函数对象的形式,更改set排序方式
// 从大到小排序的比较器函数对象struct Compartor{ bool operator()(const int lhs,const int rhs) const { return rhs < lhs; }};// 声明使用自定义比较器的setset<int,Compartor> s;// 按照从小到大的顺序插入for (int i = 0; i < 10; i++){ s.insert(i);}set<int>::iterator it;// 输出的顺序的作用是从大到小for (it = s.begin(); it != s.end(); ++it){ cout << *it ;}
stl 中set 有序的性质可以应用到同时需要过滤重复和排序的场景。
阅读全文
0 0
- c++ stl set 有序性
- C++STL之set
- C++--STL---set容器
- C++STL-list和set
- c++STL Set集合容器
- C++STL之set容器
- 有序性
- map、set、multiset、multimap的有序性
- C++STL练习<queue> <set>容器使用
- C++STL之关联容器【map】【set】
- C++STL之Set集合容器
- C++STL set介绍与使用方法
- C++::STL::容器类::set/multiset
- [C++::STL]之set的用法
- C++:STL常用函数模块总结(set)
- STL set
- STL SET
- STL set
- 爬虫豆瓣电影top250代码和收获
- MyHTMLParser加分析别人的代码
- HTML放大旋转变色扭曲
- DAY004
- js中alert()|confirm()|prompt()的用法
- c++ stl set 有序性
- 指针--类型组合
- scala学习一
- MVP
- 敲代码时的一个小疑问
- matlab图像类型转换以及uint8、double、im2double、im2uint8和mat2gray等说明
- 11.5-关于用Python建网站
- Django中关于MVC和MVT
- Boxes And Balls CodeForces