STL - 容器 - Set
2024-09-20 02:55:23
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 ----------------
最新文章
- js在IE和FF下的兼容性问题
- 《CoffeeScript应用开发》学习:第五章 CoffeeScript中的类
- ASP.Net中防止页面刷新重复提交的几种方法
- git管理maven项目实现
- nyist 593 Take it easy
- [C和指针]第二部分
- Uva 10881 Piotr&rsquo;s Ants 蚂蚁
- CLR via C# 读书笔记 6-2 不同AppDomain之间的通信 z
- RDLC(Reportview)报表直接打印,支持所有浏览器,客户可在linux下浏览使用
- Java 大数A+B
- SQL 数据开发(经典)转贴
- JavaScript基础笔记(一)基本概念
- php curl数据传输神器
- 移植marvell poncat3 demo板的总结
- 洛谷P3345 [ZJOI2015]幻想乡战略游戏(动态点分治,树的重心,二分查找,Tarjan-LCA,树上差分)
- ####### Scripts Summary #######
- cdcq的独立博客上线辣!->; http://cdcq.coding.me/blog/
- selenium元素定位Xpath,Contains,CssSelector
- 基于jQuery+CSS3实现人物跳动特效
- 我的linux shell 脚本头部
热门文章
- Poj 题目分类
- JavaScript惰性函数定义
- PAT甲级1119. Pre- and Post-order Traversals
- Mac下操作mysql
- sql prompt5安装好了,也破解完成了,然后到SQL里面还是没有提示是为什么?
- IntelliJ IDEA使用教程三 SVN的集成与使用
- 用最简单的例子理解对象为Null模式(Null Object Pattern)
- Ransac 与 最小二乘(LS, Least Squares)拟合直线的效果比较
- Http请求之基于HttpUrlConnection,支持Header,Body传值,支持Multipart上传文件:
- EF三种加载方法