1. 概述
set 的声明如下所示:
template < class T, // set::key_type/value_type
class Compare = less<T>, // set::key_compare/value_compare
class Alloc = allocator<T> // set::allocator_type
> class set;
通俗的讲,set 是一种表示集合的容器,通常用二叉树进行实现,set 对象中的元素是有序列不重复的。简单的说,可以将每个元素都看作是键与值的统一体,表示元素的同时也用来标识其自身。set 中的元素是 const 属性的,即不能在容器中进行修改,但可以将其从容器中插入和删除。在内部,set 中的元素总是按照其内部比较对象(类型为Compare)指示的特定严格弱排序标准进行排序。set 容器通常比 unordered_set 容器通过键访问单个元素要慢,但是它们允许基于其顺序的子集的直接迭代。
2. 基本操作
以下代码基本操作的角度说明了如何使用 set 容器:
2.1 对象的创建
#include <iostream>
#include <set> // 使用之前需要包含指定的头文件
bool fncomp (int lhs, int rhs) {return lhs<rhs;}
struct classcomp {
bool operator() (const int& lhs, const int& rhs) const
{return lhs<rhs;}
};
int main ()
{
std::set<int> first; // 定义一个元素类型为int的set对象
int myints[]= {10,20,30,40,50,60,70,80,90};
std::set<int> second (myints,myints+5); // 以范围的方式初始化
std::set<int> third (second); // 拷贝初始化
std::set<int> fourth (second.begin(), second.end()); // 迭代器初始化
std::set<int,classcomp> fifth; // 指定排序方式的初始化
bool(*fn_pt)(int,int) = fncomp;
std::set<int,bool(*)(int,int)> sixth (fn_pt); // 以函数指针形式指定排序方式的初始化
first = std::set<int>(); // 用空集合覆盖原对象
return 0;
}
2.1 迭代器
#include <iostream>
#include <stdio.h>
#include <set>
int main ()
{
int myints[] = { 75, 23, 65, 42, 13 };
std::set<int> myset(myints, myints + 5);
std::cout << "begin & end : ";
for (std::set<int>::iterator it = myset.begin(); it != myset.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
std::cout << "rbegin & rend : ";
for (std::set<int>::reverse_iterator it = myset.rbegin(); it != myset.rend(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
std::cout << "cbegin & cend: ";
for (auto it = myset.cbegin(); it != myset.cend(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
std::cout << "cbegin & cend: ";
for (auto rit = myset.crbegin(); rit != myset.crend(); ++rit)
std::cout << ' ' << *rit;
std::cout << '\n';
system("pause");
return 0;
}
2.3 成员函数列举
函数名称 | 函数功能 |
---|---|
empty() | 如果容器为空返回 true,否则返回 false |
size() | 返回对象中含有元素的数量 |
max_size() | 返回在当前环境下允许的最大元素容纳量 |
insert() | 插入元素 |
erase() | 清除元素 |
swap() | 交换对象内容 |
clear() | 清空容器 |
emplace() | 构建并插入元素 |
emplace_hint() | 带位置提示的构建并插入元素 |
find(x) | 查找元素 x 并返回指向其的迭代器,若不存在则返回尾迭代器 .end() |
count(x) | 计算集合中具有特定值x的元素个数 |
lower_bound(x) | 若存在大于等于x的最小元素,则返回指向此元素的迭代器,否则返回 .end() |
upper_bound(x) | 若存在严格大于x的最小元素,则返回指向此元素的迭代器,否则返回 .end() |
equal_range() | 获得相等元素所在的范围 |
get_allocator() | 获取分配器 |
3. 应用代码举例
以下代码简单的列举了一下常用成员函数的使用,如果想详细的了解可以参考 C++ Reference —— set.
#include <iostream>
#include <set>
#include <stdio.h>
int main ()
{
int myints[] = { 75, 23, 65, 42, 13 };
std::set<int> myset(myints, myints + 5);
myset.insert(20); // 元素插入
myset.insert(30);
myset.insert(10);
std::cout << "Current set size : " << myset.size() << std::endl; // 显示当前的元素数量
auto ite = myset.find(10); // 搜索元素 10
std::cout << *ite << std::endl;
std::cout << "myset contains:";
while (!myset.empty()) // 若为空则返回true,否则返回false
{
std::cout << ' ' << *myset.begin();
myset.erase(myset.begin()); // 以迭代器的形式清除当前元素
}
std::cout << '\n';
system("pause");
return 0;
}
4. 小结
因为在使用上,很多与之前总结的Map容器类似,所以只是简单的记录一下,很多内容就不再赘述了。
5. 参考引用
[1] Set
[2] #include <set>