一、概述

set 容器内的元素会被自动排序,set 与 map 不同,set 中的元素即是键值又是实值,set 不允许两个元素有相同的键值。不能通过 set 的迭代器去修改 set 元素,原因是修改元素会破坏 set 组织。当对容器中的元素进行插入或者删除时,操作之前的所有迭代器在操作之后依然有效。

二、定义及初始化

使用之前必须加相应容器的头文件:

#include <set> // set属于std命名域的,因此需要通过命名限定,例如using std::set;

定义的代码如下:

set<int> a; // 定义一个int类型的集合a
// set<int> a(10); // error,未定义这种构造函数
// set<int> a(10, 1); // error,未定义这种构造函数
set<int> b(a); // 定义并用集合a初始化集合b
set<int> b(a.begin(), a.end()); // 将集合a中的所有元素作为集合b的初始值

除此之外,还可以直接使用数组来初始化向量:

int n[] = { 1, 2, 3, 4, 5 };
list<int> a(n, n + 5); // 将数组n的前5个元素作为集合a的初值

三、基本操作

3.1 容量函数

  • 容器大小:st.size();
  • 容器最大容量:st.max_size();
  • 容器判空:st.empty();
  • 查找键 key 的元素个数:st.count(key);
#include <iostream>
#include <set> using namespace std; int main(int argc, char* argv[])
{
set<int> st;
for (int i = 0; i<6; i++)
{
st.insert(i);
} cout << st.size() << endl; // 输出:6
cout << st.max_size() << endl; // 输出:214748364
cout << st.count(2) << endl; // 输出:1
if (st.empty())
cout << "元素为空" << endl; // 未执行 return 0;
}

3.2 添加函数

  • 在容器中插入元素:st.insert(const T& x);
  • 任意位置插入一个元素:st.insert(iterator it, const T& x);
#include <iostream>
#include <set> using namespace std; int main(int argc, char* argv[])
{
set<int> st; // 在容器中插入元素
st.insert(4);
// 任意位置插入一个元素
set<int>::iterator it = st.begin();
st.insert(it, 2); // 遍历显示
for (it = st.begin(); it != st.end(); it++)
cout << *it << " "; // 输出:2 4
cout << endl; return 0;
}

3.3 删除函数

  • 删除容器中值为 elem 的元素:st.pop_back(const T& elem);
  • 删除it迭代器所指的元素:st.erase(iterator it);
  • 删除区间 [first,last] 之间的所有元素:st.erase(iterator first, iterator last);
  • 清空所有元素:st.clear();
#include <iostream>
#include <set> using namespace std; int main(int argc, char* argv[])
{
set<int> st;
for (int i = 0; i < 8; i++)
st.insert(i); // 删除容器中值为elem的元素
st.erase(4);
// 任意位置删除一个元素
set<int>::iterator it = st.begin();
st.erase(it);
// 删除[first,last]之间的元素
st.erase(st.begin(), ++st.begin()); // 遍历显示
for (it = st.begin(); it != st.end(); it++)
cout << *it << " "; // 输出:2 3 5 6 7
cout << endl; // 清空所有元素
st.clear(); // 判断set是否为空
if (st.empty())
cout << "集合为空" << endl; // 输出:集合为空 return 0;
}

3.4 访问函数

  • 查找键 key 是否存在,若存在,返回该键的元素的迭代器;若不存在,返回set.end(): st.find(key);
#include <iostream>
#include <set> using namespace std; int main(int argc, char* argv[])
{
set<int> st;
for (int i = 0; i < 6; i++)
st.insert(i); // 通过find(key)查找键值
set<int>::iterator it;
it = st.find(2);
cout << *it << endl; // 输出:2 return 0;
}

3.5 其他函数

  • 交换两个同类型容器的元素:swap(set&, set&);lst.swap(set&);
#include <iostream>
#include <set> using namespace std; int main(int argc, char* argv[])
{
set<int> st1;
st1.insert(1);
st1.insert(2);
st1.insert(3);
set<int> st2;
st2.insert(4);
st2.insert(5);
st2.insert(6);
// 交换两个容器的元素
st1.swap(st2); // 遍历显示
cout << "交换后的st1: ";
set<int>::iterator it;
for (it = st1.begin(); it != st1.end(); it++)
cout << *it << " "; // 输出:4 5 6
cout << endl;
// 遍历显示
cout << "交换后的st2: ";
for (it = st2.begin(); it != st2.end(); it++)
cout << *it << " "; // 输出:1 2 3
cout << endl; return 0;
}

四、迭代器与算法

1. 迭代器

  • 开始迭代器指针:st.begin();
  • 末尾迭代器指针:st.end(); // 指向最后一个元素的下一个位置
  • 指向常量的开始迭代器指针:st.cbegin(); // 意思就是不能通过这个指针来修改所指的内容,但还是可以通过其他方式修改的,而且指针也是可以移动的。
  • 指向常量的末尾迭代器指针:lst.cend();
  • 反向迭代器指针,指向最后一个元素:st.rbegin();
  • 反向迭代器指针,指向第一个元素的前一个元素:st.rend();
  • 返回最后一个 key<=keyElem 元素的迭代器:st.lower_bound(keyElem);
  • 返回第一个 key>keyElem 元素的迭代器: st.upper_bound(keyElem);
  • 返回容器中 key 与 keyElem 相等的上下限的两个迭代器,这两个迭代器被放在对组(pair)中: st.equal_range(keyElem);
#include <iostream>
#include <set> using namespace std; int main(int argc, char* argv[])
{
set<int> st;
st.insert(1);
st.insert(2);
st.insert(3); cout << *(st.begin()) << endl; // 输出:1
cout << *(--st.end()) << endl; // 输出:3
cout << *(st.cbegin()) << endl; // 输出:1
cout << *(--st.cend()) << endl; // 输出:3
cout << *(st.rbegin()) << endl; // 输出:3
cout << *(--st.rend()) << endl; // 输出:1
cout << *(st.lower_bound(2)) << endl; // 输出:2
cout << *(st.upper_bound(2)) << endl; // 输出:3
pair<set<int>::iterator, set<int>::iterator> t_pair = st.equal_range(2);
cout << *(t_pair.first) << endl; // 输出:2
cout << *(t_pair.second) << endl; // 输出:3
cout << endl; return 0;
}

2. 算法

  • 遍历元素
set<int>::iterator it;
for (it = st.begin(); it != st.end(); it++)
cout << *it << endl;

三、总结

可以看到,set 与序列式容器的用法有以下几处不同:

  • set 不支持 resize() 函数;

  • set 容器不提供下标操作符。为了通过键从 set 中获取元素,可使用 find 运算;

  • set 只能使用insert的两种重载函数插入,不支持 push_back() 和 push_front() 函数;

  • set 不支持 STL 里的 reverse 和 sort 算法;

  • set 能不通过迭代器,只通过元素值来删除该元素;

  • set 只支持默认构造函数和拷贝构造函数,没有其它重载的构造函数。

[C++ STL] set使用详解的更多相关文章

  1. STL bind1st bind2nd详解

    STL bind1st bind2nd详解   先不要被吓到,其实这两个配接器很简单.首先,他们都在头文件<functional>中定义.其次,bind就是绑定的意思,而1st就代表fir ...

  2. C++ STL bitset 容器详解

    C++ STL bitset 容器详解 本篇随笔讲解\(C++STL\)中\(bitset\)容器的用法及常见使用技巧. \(bitset\)容器概论 \(bitset\)容器其实就是个\(01\)串 ...

  3. 2.3 C++STL vector容器详解

    文章目录 2.3.1 引入 2.3.2 代码实例 2.3.3 运行结果 总结 2.3.1 引入 vector 容器 动态数组 可变数组 vector容器 单口容器(尾部操作效率高) vector动态增 ...

  4. [GeekBand] STL 仿函数入门详解

    本文参考文献::GeekBand课堂内容,授课老师:张文杰 :C++ Primer 11 中文版(第五版) page 37 :网络资料: 叶卡同学的部落格  http://www.leavesite. ...

  5. C++ STL之vector详解

    转自http://blog.sina.com.cn/s/blog_9f1c0931010180cy.html Vectors   vector是C++标准模板库中的部分内容,它是一个多功能的,能够操作 ...

  6. C++ STL Binary search详解

    一.解释 以前遇到二分的题目都是手动实现二分,不得不说错误比较多,关于返回值,关于区间的左闭右开等很容易出错,最近做题发现直接使用STL中的二分函数方便快捷还不会出错,不过对于没有接触过的同学,二分函 ...

  7. C++ STL之LIST详解A

    List 容器 list是C++标准模版库(STL,Standard Template Library)中的部分内容.实际上,list容器就是一个双向链表,可以高效地进行插入删除元素. 使用list容 ...

  8. Qt迭代器(Java类型和STL类型)详解

    迭代器为访问容器类里的数据项提供了统一的方法,Qt 有两种迭代器类:Java 类型的迭代器和 STL 类型的迭代器. 两者比较,Java 类型的迭代器更易于使用,且提供一些高级功能,而 STL 类型的 ...

  9. 2.2 C++STL string容器详解

    文章目录 引言 2.2.1 string的特性 2.2.2 string用法理论 2.2.2.1 string构造函数 2.2.2.2 string赋值操作 2.2.2.3 string取值操作 2. ...

随机推荐

  1. poj_2586_Y2K Accounting Bug_201407211318

    Y2K Accounting Bug Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10210   Accepted: 50 ...

  2. Minimum Path Sum(DFS,DP)

    Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...

  3. JVM定位程序假死或cpu占用高的线程

    linux系统: 参考:https://blog.csdn.net/qq_40197576/article/details/80287515 1>使用top命令查看占用cpu进程情况,得到jav ...

  4. java.util.Scanner

    java.util.Scanner是Java5的新特征,主要功能是简化文本扫描.这个类最实用的地方表现在获取控制台输入,其他的功能都很鸡肋,尽管Java API文档中列举了大量的API方法,但是都不怎 ...

  5. maven打包插件maven-shade-plugin简单介绍

    作用: 1.可以把依赖打入jar包,然后直接使用这个jar包,从而不用担心依赖问题 2.通过设置MainClass,创建一个可以执行的jar包 3.Java工程经常会遇到第三方 Jar 包冲突,使用 ...

  6. Web端口复用正向后门研究实现与防御

    0×01背景 现在的很多远控/后门因为目前主流防火墙规则的限制,基本上都采用TCP/UDP反弹回连的通讯形式:但是在较高安全环境下,尤其负责web相关业务的环境,因为安防设备(防火墙,IDS,IPS等 ...

  7. System.AccessViolationException”类型的未经处理的异常在 System.Data.dll 中发生。其它信息:尝试读取或写入受保护的内存。这通常指示其它内存已损坏。

    错误背景: 操作系统:编程环境:VS2013.  语言:VB.net:  数据库:SQLserver2008 做数据库连接时.发生的错误: 错误提示为: 说明:用VB.net连接SQLServer数据 ...

  8. 网络最大流增广路模板(EK &amp; Dinic)

    EK算法: int fir[maxn]; int u[maxm],v[maxm],cap[maxm],flow[maxm],nex[maxm]; int e_max; int p[maxn],q[ma ...

  9. EEPlat的控制器概念

    控制器是EEPlat平台界面层部分的核心概念.平台中界面展示都是通过平台的各种控制器综合控制输出的. EEPlat平台的界面层模型採用了HMVC模式.HMVC模式的採用使得EEPlat平台界面层可以实 ...

  10. Brackets常用插件

    Emmet插件:https://github.com/emmetio/brackets-emmet AngularJS插件:https://github.com/angular-ui/AngularJ ...