STL容器之set
【1】set容器
一个集合(set)是一个容器,它其中所包含的元素的值是唯一的。
【2】set容器方法
(1)set构造函数、插入函数、遍历过程
应用示例代码如下:
#include <set>
#include <iostream>
using namespace std; bool funcComp(int lhs, int rhs)
{
return lhs > rhs;
} struct classcomp
{
bool operator() (const int& lhs, const int& rhs) const
{
return lhs < rhs;
}
};
// 正向遍历
void print(const set<int> & lessSet)
{
set<int>::iterator iter = lessSet.begin();
for (; iter != lessSet.end(); ++iter)
{
cout << (*iter) << " ";
}
cout << endl;
}
// 反向遍历
void print(const set<int, greater<int>> & greaterSet)
{
set<int, greater<int>>::reverse_iterator ritor;
ritor = greaterSet.rbegin();
while (ritor != greaterSet.rend())
{
cout << (*ritor) << " ";
++ritor;
}
cout << endl;
} void print(const set<int, bool(*)(int, int)> & funcpSet)
{
set<int, bool(*)(int, int)>::iterator iter = funcpSet.begin();
for (; iter != funcpSet.end(); ++iter)
{
cout << (*iter) << " ";
}
cout << endl;
} void print(const set<int, classcomp> & classSet)
{
set<int, classcomp>::iterator iter = classSet.begin();
for (; iter != classSet.end(); ++iter)
{
cout << (*iter) << " ";
}
cout << endl;
} void main ()
{
// 1.默认构造函数创建一个空的set容器
set<int> first;
int n = ;
while (n <= )
{
first.insert(n++);
}
cout << "打印first容器的值:" << endl;
print(first); // 2.默认是以小于比较器less<int>创建的,再创建一个带大于比较器的set
set<int, greater<int>> second;
n = ;
while (n <= )
{
second.insert(n++);
}
cout << "打印second容器的值:" << endl;
print(second); // 3.用数组元素值创建一个容器
int myInts[] = {, , , , };
set<int> third(myInts, myInts + );
cout << "打印third容器的值:" << endl;
print(third); // 4.调用拷贝构造函数创建一个容器
set<int> fourth(third);
cout << "打印fourth容器的值:" << endl;
print(fourth); // 5.由已知对象的区间创建一个容器
set<int> fifth(first.begin(), first.end());
cout << "打印fifth容器的值:" << endl;
print(fifth); // 6.以函数指针为比较器
bool(*func_pt)(int, int) = funcComp;
set<int, bool(*)(int, int)> sixth(func_pt);
for (int i = ; i < ; ++i)
{
sixth.insert(rand() % );
}
cout << "打印sixth容器的值:" << endl;
print(sixth); // 7.以仿函数为比较器
set<int, classcomp> seventh;
for (int i = ; i < ; ++i)
{
seventh.insert(rand() % );
}
cout << "打印seventh容器的值:" << endl;
print(seventh); system("pause");
} // run out:
/*
打印first容器的值:
1 2 3 4 5 6 7 8 9 10
打印second容器的值:
10 11 12 13 14 15 16 17 18 19 20
打印third容器的值:
10 20 30 40 50
打印fourth容器的值:
10 20 30 40 50
打印fifth容器的值:
1 2 3 4 5 6 7 8 9 10
打印sixth容器的值:
78 69 67 64 62 58 41 34 24 0
打印seventh容器的值:
5 27 36 42 45 61 81 91 95
请按任意键继续. . .
*/
(2)插入、大小、判空、最大个数等等
示例代码如下:
#include <set>
#include <iostream>
using namespace std; // 正向遍历
void print(const set<int> & lessSet)
{
set<int>::iterator iter = lessSet.begin();
while (iter != lessSet.end())
{
cout << (*iter) << " ";
iter++;
}
cout << endl;
} void main()
{
set<int> myset;
set<int>::iterator it;
pair<set<int>::iterator, bool> ret;
// 插入数据元素
for (int i = ; i <= ; ++i)
{
myset.insert(i * ); // 元素为: 10 20 30 40 50
} ret = myset.insert(); // 再插入20,发现已存在,则插入操作失败!
if (false == ret.second)
it = ret.first; // it迭代器指向了20这个元素 myset.insert (it, );
myset.insert (it, );
myset.insert (it, ); int myints[] = {, , }; // 10已经在容器中
myset.insert(myints, myints + );
cout << "打印mySet容器的值:" << endl;
print(myset); set<int> firstSet;
for (int i = ; i < ; ++i)
{
firstSet.insert(rand() % );
}
cout << "打印firstSet容器的值:" << endl;
print(firstSet); cout << "empty():" << firstSet.empty() << endl;
cout << "size():" << firstSet.size() << endl;
cout << "max_size():" << firstSet.max_size() << endl; system("pause");
} // run out:
/*
打印mySet容器的值:
5 10 15 20 24 25 26 30 40 50
打印firstSet容器的值:
0 24 34 41 58 62 64 67 69 78
empty():0
size():10
max_size():1073741823
请按任意键继续. . .
*/
(3)删除、清空、交换
示例代码如下:
#include <set>
#include <iostream>
using namespace std; // 正向遍历
void print(const set<int> & lessSet)
{
set<int>::iterator iter = lessSet.begin();
while (iter != lessSet.end())
{
cout << (*iter) << " ";
iter++;
}
cout << endl;
} void main ()
{
set<int> mySet;
set<int>::iterator it;
for (int i = ; i < ; i++)
mySet.insert(i * ); // 10 20 30 40 50 60 70 80 90
cout << "打印mySet容器数据:" << endl;
print(mySet); it = mySet.begin();
++it;
// 第一种删除方式
mySet.erase(it);
// 第二种删除方式
mySet.erase();
it = mySet.find();
// 第三种删除方式
mySet.erase(it, mySet.end());
cout << "删除后,打印mySet容器数据:" << endl;
print(mySet); set<int> firstSet;
for (int i = ; i < ; ++i)
{
firstSet.insert((i + ) * );
}
cout << "打印firstSet容器的值:" << endl;
print(firstSet);
// 第四种删除
set<int>::iterator iter = firstSet.begin();
for (; iter != firstSet.end(); )
{
if ((*iter) == )
{
firstSet.erase(iter++);
}
else
{
++iter;
}
}
cout << "删除50后,打印firstSet容器的值:" << endl;
print(firstSet); mySet.clear(); // 清空
firstSet.swap(mySet); // 交换两个容器
cout << "交换后,打印mySet容器的值:" << endl;
print(mySet);
cout << "交换后,打印firstSet容器的值:" << endl;
print(firstSet); system("pause");
} //run out:
/*
打印mySet容器数据:
10 20 30 40 50 60 70 80 90
删除后,打印mySet容器数据:
10 30 50
打印firstSet容器的值:
30 40 50 60 70 80 90 100 110
删除50后,打印firstSet容器的值:
30 40 60 70 80 90 100 110
交换后,打印mySet容器的值:
30 40 60 70 80 90 100 110
交换后,打印firstSet容器的值: 请按任意键继续. . .
*/
(4)key_comp函数
函数返回比较函数对象,默认的是升序排列。
示例代码如下:
#include <set>
#include <iostream>
using namespace std; // 正向遍历
void print(const set<int> & lessSet)
{
set<int>::iterator iter = lessSet.begin();
while (iter != lessSet.end())
{
cout << (*iter++) << " ";
}
cout << endl;
} void main ()
{
set<int> mySet;
int highest;
set<int>::key_compare myComp = mySet.key_comp();
for (int i = ; i <= ; ++i)
{
mySet.insert((i + ) * );
} cout << "打印mySet容器中的数据元素:" << endl;
print(mySet); cout << "利用比较函数打印容器中小于最大值的元素:" << endl;
highest = *mySet.rbegin();
set<int>::iterator it = mySet.begin();
do
{
cout << " " << *it;
} while (myComp(*(++it), highest));
cout << endl; system("pause");
} // run out:
/*
打印mySet容器中的数据元素:
10 20 30 40 50 60
利用比较函数打印容器中小于最大值的元素:
10 20 30 40 50
请按任意键继续. . .
*/
(5)count函数。函数返回值为val的元素的个数,当然在set容器中其要么为0,要么为1。
示例代码如下:
#include <set>
#include <iostream>
using namespace std; void main ()
{
set<int> mySet;
// set some initial values:
for (int i = ; i < ; ++i)
mySet.insert(i * ); // set: 3 6 9 12 if (mySet.count() == )
cout << " 9 is an element of myset.\n";
else
cout << " 9 is not an element of myset.\n"; system("pause");
}
// run out:
/*
9 is an element of myset.
请按任意键继续. . .
*/
(6)lower_bound 和 upper_bound函数
lower_bound 函数返回set中第一个小于或者等于val的元素的iterator。
upper_bound 函数返回set中第一个大于或者等于val的元素的iterator。
示例代码如下:
#include <set>
#include <iostream>
using namespace std; void main ()
{
set<int> mySet;
set<int>::iterator itlow, itup;
for (int i = ; i < ; i++)
mySet.insert(i * ); // 10 20 30 40 50 60 70 80 90 itlow = mySet.lower_bound ();
itup = mySet.upper_bound ();
mySet.erase(itlow, itup); // 10 20 70 80 90 cout << "mySet contains:";
for (set<int>::iterator it = mySet.begin(); it != mySet.end(); ++it)
{
cout << " " << *it;
} cout << endl; system("pause");
}
// run out:
/*
mySet contains: 10 20 70 80 90
请按任意键继续. . .
*/
(7)equal_range 函数返回等于set中val的上下界的iterator。
示例代码如下:
#include <set>
#include <iostream>
using namespace std; void main ()
{
set<int> mySet;
for (int i = ; i <= ; ++i)
mySet.insert(i * ); // mySet: 10 20 30 40 50 pair<set<int>::const_iterator, set<int>::const_iterator> ret;
ret = mySet.equal_range(); cout << "the lower bound points to: " << (*ret).first << endl;
cout << "the upper bound points to: " << (*ret).second << endl; system("pause");
} // run out:
/*
the lower bound points to: 30
the upper bound points to: 40
请按任意键继续. . .
*/
(8)get_allocator 函数返回set的分配器对象。
示例代码如下:
#include <set>
#include <iostream>
using namespace std; void main ()
{
set<int> myset;
int* p = NULL;
unsigned int i; // allocate an array of 5 elements using myset's allocator:
p = myset.get_allocator().allocate(); // assign some values to array
for (i = ; i < ; ++i)
p[i] = (i + ) * ; cout << "The allocated array contains:";
for (i = ; i < ; ++i)
cout << ' ' << p[i]; cout << endl; myset.get_allocator().deallocate(p, ); system("pause");
} // run out:
/*
The allocated array contains: 10 20 30 40 50
请按任意键继续. . .
*/
(9)待续......
【3】set容器总结
一个集合(set)是一个容器,它其中所包含的元素的值是唯一的。
Good Good Study, Day Day Up.
顺序 选择 循环 总结
STL容器之set的更多相关文章
- [知识点]C++中STL容器之map
UPDATE(20190416):写完vector和set之后,发现不少内容全部引导到map上了……于是进行了一定的描述补充与更正. 零.STL目录 1.容器之map 2.容器之vector 3.容器 ...
- [知识点]C++中STL容器之set
零.STL目录 1.容器之map 2.容器之vector 3.容器之set 一.前言 继上期的vector之后,我们又迎来了另一个类数组的STL容器——set. 二.用途与特性 set,顾名思义,集合 ...
- [知识点]C++中STL容器之vector
零.STL目录 1.容器之map 2.容器之vector 3.容器之set 一.前言 关于STL和STL容器的概念参见STL系列第一篇——map(见上).今天介绍第二个成员——vector. 二.用途 ...
- STL容器之map
[1]map容器 map 是关联容器.容器中的每一个元素都是由一个键值和一个数据值组成的. set 是一个集合它以其元素作为键值(同一个键值只能出现一次),且默认以升序排列. list 是一个顺序容器 ...
- STL容器之vector
[1]模板类vector 模板类vector可理解为广义数组.广义数组,即与类型无关的数组,具有与数组相同的所有操作. 那么,你或许要问:既然C++语言本身已提供了一个序列式容器array,为什么还要 ...
- C++ STL容器之 stack
STL 中的 stack 是一种容器适配器,而不是一种容器. 它是容器适配器是指,只要支持一系列方法的容器(empty, size, back, push_back, pop_back),都能作为st ...
- STL容器之list
[1]list简介 实质上,list容器就是一个双向链表,可以高效地进行插入.删除操作. [2]list链表常用方法 (1)构造.赋值.清空.删除.插入.判空等 应用示例代码如下: #include ...
- STL容器之deque
[1]deque容器 deque 是对 vector 和 list 优缺点的结合,它是处于两者之间的一种容器. [2]deque方法集 应用示例代码: #include <deque> # ...
- STL容器之Array[转]
转自https://blog.csdn.net/sin_geek/article/details/51067874 作者 Sin_Geek 简介 array在头文件<array> 中定义 ...
随机推荐
- java 随机抽取案例,不重复抽取
以学生类为例,先准备一个Student类 package cn.sasa.demo1; public class Student { private int id; private String na ...
- C# Asp.net 修改Ueditor编辑器上传图片保存路径
默认情况下Ueditor图片上传的保存路径是在/ueditor/net/upload/目录下,(如:http://localhost/ueditor/net/upload/123.png), 但是有时 ...
- atom 为什么启动terminal是总是打开是用户目录?
atom 为什么启动terminal是总是打开是用户目录?如下图: 原因也很简单,只怪自己懒,没查单词surpress是什么意思: surpress directory argument,是抑制目录参 ...
- MySQL crash-safe replication【转载】
本文来自david大神的博客,innodb技术内幕的作者. http://insidemysql.blog.163.com/blog/static/202834042201385190333/ MyS ...
- 深入SQL Server优化【推荐】
深入sql server优化,MSSQL优化,T-SQL优化,查询优化 十步优化SQL Server 中的数据访问故事开篇:你和你的团队经过不懈努力,终于使网站成功上线,刚开始时,注册用户较少,网站性 ...
- 前端 HTML body标签相关内容 常用标签 表单标签 form
表单标签 form 表单是一个包含表单元素的区域表单元素是允许用户在表单中输入内容,比如:文本域(textarea).输入框(input).单选框() 表单的作用 form标签作用是把用户输入数据信息 ...
- 前端 HTML body标签相关内容 常用标签 盒子标签 div
盒子标签 div <div>可定义文档的分区 division的缩写 译:区 <div> 标签可以把文档分割为独立的.将他们进行分区 div在浏览器中,默认是不会增加任何的效果 ...
- 20171018 微信小程序客户端数据和服务器交互
-- 时常在想,怎么样才能把知识写的清晰,其实是我理解的不够清晰 微信小程序其实是一个客户端页面,也是需要和服务器交互才能体现数据. 1 --服务器搭建Web API :MVC4 中的一个模板, 如下 ...
- 云锁-安全,易用,灵活的许可-Virbox许可管理平台
云许可,助力开发者保护软件财富 Virbox 云锁提供高强度加密及软件许可管理功能 安全,易用,灵活 防破解 云许可安全性体验与硬件锁一致,避免盗版导致损失 加密保护技术:反黑引擎/碎片代码执行/虚拟 ...
- 小程序 login
app.json : 配置文件 => 文件路径 pages .配置窗口 window.底部导航 tabBar .请求超时时间 networkTimeout app.js : 请求路口文件 wx. ...