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里面的散列函数和相等函数怎么写.学习过程中看到了一个好帖子 ...
随机推荐
- euclidea 3.0 全三星 攻略
euclidea攻略 游戏地址 http://www.euclidea.xyz/en/game/#/packs 攻略 Alpha level : 1.1 line tool 3L3E 智障题 1.2 ...
- 彻底解决每次打开visio都提示windows正在配置visio的问题
出现这个提示windows正在配置XXX软件的问题,是由于在安装一个新的版本时,之前那个版本的office没有完全卸载,注册表内有残留. 最简单的方式,并不是 把HKEY_CURRENT_USER\S ...
- angularjs中如何在异步请求执行完以后再执行其他函数?
angularjs中如何在异步请求执行完以后再执行其他函数? 之前脑袋回路就是从上到下的执行js,直到有一次我的页面上已经显示了空才走到angularjs里的$http的成功回调函数里,然后才开始正视 ...
- CentOS使用chkconfig增加开机服务提示service xxx does not support chkconfig的问题解决
在shell文件的第二行增加如下内容即可: # chkconfig: 2345 10 90 #服务必须在运行级2,3,4,5下被启动或关闭,启动的优先级是90,关闭的优先级是10. # descrip ...
- solaris 常用软件安装
http://blog.csdn.net/cuterhei/article/category/1259722
- mybatis配置文件,注意标签配置顺序。否则报错The content of element type "configuration" must match "(properties?,settings?,...怎么解决
感谢原作者http://www.cnblogs.com/zhoumingming/p/5417014.html 注意每个标签必须按照顺序写,不然就会提示错误 顺序是 <?xml version= ...
- 在Windows下编译Emacs
在Windows下编译Emacs Windows下编译好的Emacs主要有两个版本,一个来自http://nqmacs.sourceforge.net/,另一个来自http://www.crasseu ...
- 在VS 2010上搭建Windows Phone 7开发平台
如今Windows Phone 7平台越来越火了,刚刚拿到一款新的Windows Phone,于是准备在电脑上搭建WP7的开发环境. 首先,安装VS2010,升级到SP1,并安装Windows P ...
- 如何让两个div并排,并且div要看得见边框
<div style="float:left; width:100px; height:100px; border:2px solid #0000FF;"></d ...
- 《JavaScript启示录》
<JavaScript启示录> 基本信息 作者: (美)Cody Lindley 译者: 徐涛 出版社:人民邮电出版社 ISBN:9787115334947 上架时间:2014-2-21 ...