Set根据特定排序准则,自动将元素排序。

Set不允许元素重复。

一些常规操作:

SetTest.cpp

#include <iostream>
#include <set>
#include <algorithm>
#include <iterator>
#include <functional>
#include "SetTest.h" using namespace std; void SetTest::operationDemo()
{
// type of the collection:
// - no duplicates
// - elements are integral values
// - descending order
set<int, greater<int>> coll1; // insert elements in random order using different member functions
coll1.insert({ , , , , , });
coll1.insert(); // print all elements
for (int elem : coll1)
{
cout << elem << ' ';
}
cout << endl; // insert 4 again and process return value
auto status = coll1.insert();
if (status.second)
{
cout << "4 inserted as element "
<< distance(coll1.begin(), status.first) + << endl;
}
else
{
cout << "4 already exists" << endl;
} // assign elements to another set with ascending order
set<int> coll2(coll1.cbegin(), coll1.cend()); // print all elements of the copy using stream iterators
copy(coll2.cbegin(), coll2.cend(), ostream_iterator<int>(cout, " "));
cout << endl; // remove all elements up to element with value 3
coll2.erase(coll2.begin(), coll2.find()); // remove all elements with value 3
int num;
num = coll2.erase();
cout << num << " element(s) removed" << endl; // print all elements
copy(coll2.cbegin(), coll2.cend(), ostream_iterator<int>(cout, " "));
cout << endl;
} void SetTest::run()
{
printStart("operationDemo()");
operationDemo();
printEnd("operationDemo()");
}

运行结果:

---------------- operationDemo(): Run Start ----------------
6 5 4 3 2 1
4 already exists
1 2 3 4 5 6
1 element(s) removed
4 5 6
---------------- operationDemo(): Run End ----------------

最新文章

  1. js在IE和FF下的兼容性问题
  2. 《CoffeeScript应用开发》学习:第五章 CoffeeScript中的类
  3. ASP.Net中防止页面刷新重复提交的几种方法
  4. git管理maven项目实现
  5. nyist 593 Take it easy
  6. [C和指针]第二部分
  7. Uva 10881 Piotr&rsquo;s Ants 蚂蚁
  8. CLR via C# 读书笔记 6-2 不同AppDomain之间的通信 z
  9. RDLC(Reportview)报表直接打印,支持所有浏览器,客户可在linux下浏览使用
  10. Java 大数A+B
  11. SQL 数据开发(经典)转贴
  12. JavaScript基础笔记(一)基本概念
  13. php curl数据传输神器
  14. 移植marvell poncat3 demo板的总结
  15. 洛谷P3345 [ZJOI2015]幻想乡战略游戏(动态点分治,树的重心,二分查找,Tarjan-LCA,树上差分)
  16. ####### Scripts Summary #######
  17. cdcq的独立博客上线辣!-&gt; http://cdcq.coding.me/blog/
  18. selenium元素定位Xpath,Contains,CssSelector
  19. 基于jQuery+CSS3实现人物跳动特效
  20. 我的linux shell 脚本头部

热门文章

  1. Poj 题目分类
  2. JavaScript惰性函数定义
  3. PAT甲级1119. Pre- and Post-order Traversals
  4. Mac下操作mysql
  5. sql prompt5安装好了,也破解完成了,然后到SQL里面还是没有提示是为什么?
  6. IntelliJ IDEA使用教程三 SVN的集成与使用
  7. 用最简单的例子理解对象为Null模式(Null Object Pattern)
  8. Ransac 与 最小二乘(LS, Least Squares)拟合直线的效果比较
  9. Http请求之基于HttpUrlConnection,支持Header,Body传值,支持Multipart上传文件:
  10. EF三种加载方法