STL - 容器 - Set
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 ----------------
STL - 容器 - Set的更多相关文章
- STL容器
啦啦啦,今天听啦高年级学长讲的STL容器啦,发现有好多东西还是有必要记载的,毕竟学长是身经百战的,他在参加各种比赛的时候积累的经验可不是一天两天就能学来的,那个可是炒鸡有价值的啊,啊啊啊啊啊 #inc ...
- c++ stl容器set成员函数介绍及set集合插入,遍历等用法举例
c++ stl集合set介绍 c++ stl集合(Set)是一种包含已排序对象的关联容器.set/multiset会根据待定的排序准则,自动将元素排序.两者不同在于前者不允许元素重复,而后者允许. 1 ...
- STL容器删除元素的陷阱
今天看Scott Meyers大师的stl的用法,看到了我前段时间犯的一个错误,发现我写的代码和他提到错误代码几乎一模一样,有关stl容器删除元素的问题,错误的代码如下:std::vector< ...
- 【转】c++中Vector等STL容器的自定义排序
如果要自己定义STL容器的元素类最好满足STL容器对元素的要求 必须要求: 1.Copy构造函数 2.赋值=操作符 3.能够销毁对象的析构函数 另外: 1. ...
- GDB打印STL容器内容
GDB调试不能打印stl容器内容,下载此文件,将之保存为~/.gdbinit就可以使用打印命令了. 打印list用plist命令,打印vector用pvector,依此类推. (gdb) pvecto ...
- STL容器迭代器失效分析
连续内存序列容器(vector, string, deque) 对于连续内存序列STL容器,例如vector,string,deque,删除当前iterator会使得后面所有的iterator都失效, ...
- STL容器的适用情况
转自http://hsw625728.blog.163.com/blog/static/3957072820091116114655254/ ly; mso-default-props:yes; m ...
- STL容器的遍历删除
STL容器的遍历删除 今天在对截包程序的HashTable中加入计时机制时,碰到这个问题.对hash_map中的每个项加入时间后,用查询函数遍历hash_map,以删除掉那些在表存留时间比某个阈值长的 ...
- STL容器与配接器
STL容器包括顺序容器.关联容器.无序关联容器 STL配接器包括容器配接器.函数配接器 顺序容器: vector 行为类似于数组,但可以根据要求 ...
- STL容器的本质
http://blog.sina.com.cn/s/blog_4d3a41f40100eof0.html 最近在学习unordered_map里面的散列函数和相等函数怎么写.学习过程中看到了一个好帖子 ...
随机推荐
- 更好的浏览器动画实现 requestAnimationFrame
requestAnimationFrame 是专门为实现高性能的帧动画而设计的一个API: js一般是借助setTimeout或setInterval这两个函数实现动画,性能不佳. css3动画,性能 ...
- Codeforces Round #358 (Div. 2) C. Alyona and the Tree 水题
C. Alyona and the Tree 题目连接: http://www.codeforces.com/contest/682/problem/C Description Alyona deci ...
- tyvj 1031 热浪 最短路
热浪 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://tyvj.cn/p/1031 Description 德克萨斯纯朴的民眾们这个夏天正在遭受 ...
- 使用命令行编译和运行 c、Java和python程序
集成开发环境已经非常方便,从编写程序到执行程序看到结果,让我们不用关心中间的过程.但是使用原始的.命令的方式来将程序编译运行有的时候可能有些用,比如写个简答的程序,或者是身边没有集成工具的时候. C语 ...
- 推荐一个简洁优雅的博客系统,farbox
这是我用farbox搞的一个博客:http://www.jsnull.com/ 特点: 1.无数据库,数据存在dropbox里,需要自己注册一个dropbox帐号 2.静态文本文件即是文章,可以在任何 ...
- C++反汇编-菱形继承
学无止尽,积土成山,积水成渊-<C++反汇编与逆向分析技术揭秘> 读书笔记.马上就要出差了,回来后接着写吧. 一.概述 菱形继承是最复杂的对象结构,菱形结构会将单一继承与多重继承进行组合. ...
- Git提交空文件夹的技巧
这个只能说是技巧不能说是方法,原理是在每个空文件夹新建一个.gitignore文件,然后提交. 快捷命令: find . -type d -empty -exec touch {}/.gitignor ...
- MySQL命令行工具各功能说明(转)
MySQL 服务器端使用工具程序 mysqld - SQL 后台程序(即 MySQL 服务器进程).该程序必须启动运行,才能连接服务器来访问数据库. mysqld_safe - 服务器启动脚本,可以通 ...
- Short Circuit Protection Circuit
http://www.daycounter.com/Circuits/Short-Circuit-Protection/Short-Circuit-Protection.phtml Short cir ...
- 在IOS 模拟器中 输入中文
模拟器默认的配置种没有“小地球”,只能输入英文.加入中文方法如下: 找到模拟器的Settings--->General-->Keyboard-->International KeyB ...