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> 中定义 ...
随机推荐
- python QT 编程之路
pyQT4 的Wheel 下载 https://www.lfd.uci.edu/~gohlke/pythonlibs/#pyqt4 python发送GET 或者 POST请求 https://www ...
- JavaScript的cookie和sessionStorage 、localStorage
localStorage.sessionStorage和cookie的区别与用法请见下面的博客: https://segmentfault.com/a/1190000012057010 cookie的 ...
- 关于mobilesroll使用方法的再次声明
js $('#name').click(function(valueText) { $('#demo').mobiscroll('show'); // return false; }); $(&quo ...
- Scala的apply unapply unapplySeq 语法糖
apply 可以理解为注入 unapply unapplySeq 可以理解为提取 apply 与 unapply 虽然名字相近,但是使用起来区别挺大.apply有点像构造函数unapply主要是结合模 ...
- Scala数据类型的继承结构
Scala中,所有的值都是类对象,而所有的类,包括值类型,都最终继承自一个统一的根类型Any.统一类型,是Scala的又一大特点.更特别的是,Scala中还定义了几个底层类(Bottom Class) ...
- mapper映射文件不发布
mapper映射文件不发布的问题:在pom.xml中配置,指定加载哪些资源 <resources> <resource> <directory>src/main/j ...
- elasticsearch 处理index 一直INITIALIZING状态
elasticsearch一个节点异常重启后有一个index恢复的过程中状态一直INITIALIZING 处理方法 PUT index_name/_settings { "index&quo ...
- 2018-2019-1 20189221《Linux内核原理与分析》第二周作业
读书报告 <庖丁解牛Linux内核分析> 第 1 章 计算工作原理 1.1 存储程序计算机工作模型 1.2 x86-32汇编基础 1.3汇编一个简单的C语言程序并分析其汇编指令执行过程 因 ...
- JDBC 接口学习
说明:文章所有内容皆选自实验楼教程[JDBC 入门教程],想要学习更多JDBC,可以点击教程进行学习~ JDBC 简介 JDBC 的全称是 Java Database Connectivity,叫做 ...
- [Java in NetBeans] Lesson 17. File Input/Output.
这个课程的参考视频和图片来自youtube. 主要学到的知识点有: We want to handle the bad Error. (e.g bad input / bugs in program) ...