定义于头文件 <set>
template< class Key, | (1) | |
namespace pmr { template <class Key, class Compare = std::less<Key>> | (2) | (C++17 起) |
std::set
是关联容器,含有 Key
类型对象的已排序集。用比较函数 比较 (Compare) 进行排序。搜索、移除和插入拥有对数复杂度。 set
通常以红黑树实现。
在每个标准库使用比较 (Compare) 概念的场所,用等价关系确定唯一性。不精确地说,若二个对象 a
与 b
相互间既不比较大于亦不比较小于: !comp(a, b) && !comp(b, a)
,则认为它们等价。
std::set
满足容器 (Container) 、具分配器容器 (AllocatorAwareContainer) 、关联容器 (AssociativeContainer) 和可逆容器 (ReversibleContainer) 的要求。
修改器
原位构造元素
std::set<Key,Compare,Allocator>::emplace
template< class... Args > | (C++11 起) |
若容器中无拥有该关键的元素,则插入以给定的 args
原位构造的新元素到容器。
细心地使用 emplace
允许在构造新元素的同时避免不必要的复制或移动操作。 准确地以与提供给 emplace
者相同的参数,通过 std::forward<Args>(args)... 转发调用新元素的构造函数。 即使容器中已有拥有该关键的元素,也可能构造元素,该情况下新构造的元素将被立即销毁。
没有迭代器或引用被非法化。
参数
args | - | 要转发给元素构造函数的参数 |
返回值
返回由指向被插入元素,或若不发生插入则为既存元素的迭代器,和指代插入是否发生的 bool (若发生插入则为 true ,否则为 false )。
异常
若任何操作抛出异常,则此函数无效果。
复杂度
与容器大小成对数。
使用提示原位构造元素
std::set<Key,Compare,Allocator>::emplace_hint
template <class... Args> | (C++11 起) |
插入新元素到容器中尽可能接近于恰在 hint
前的位置。原位构造元素,即不进行复制或移动操作。
以提供给函数的参数准确相同者,以 std::forward<Args>(args)... 转发调用元素的构造函数。
没有迭代器或引用被非法化。
参数
hint | - | 指向新元素将插入到其前的位置的迭代器 |
args | - | 转发给元素构造函数的参数 |
返回值
返回指向新插入元素的迭代器。
若因元素已存在而插入失败,则返回指向拥有等价关键的既存元素的迭代器。
异常
若任何操作抛出异常,则此函数无效果(强异常保证)。
复杂度
通常与容器大小成对数,但若新元素正好被插入到 hint
之前则为均摊常数。
交换内容
std::set<Key,Compare,Allocator>::swap
void swap( set& other ); | (C++17 前) | |
void swap( set& other ) noexcept(/* see below */); | (C++17 起) |
将内容与 other
的交换。不在单个元素上调用任何移动、复制或交换操作。
所有迭代器和引用保持合法。尾后迭代器被非法化。
Pred
对象必须可交换 (Swappable) ,并用非成员 swap
的非限定调用交换它们。
若 std::allocator_traits<allocator_type>::propagate_on_container_swap::value 为 true ,则用非成员 | (C++11 起) |
参数
other | - | 要与之交换内容的容器 |
返回值
(无)
异常
任何 | (C++17 前) |
noexcept 规定: noexcept(std::allocator_traits<Allocator>::is_always_equal::value | (C++17 起) |
复杂度
常数。
调用示例
#include <iostream>
#include <forward_list>
#include <string>
#include <iterator>
#include <algorithm>
#include <functional>
#include <time.h>
#include <set>
using namespace std;
struct Cell
{
int x;
int y;
Cell() = default;
Cell(int a, int b): x(a), y(b) {}
Cell &operator +=(const Cell &cell)
{
x += cell.x;
y += cell.y;
return *this;
}
Cell &operator +(const Cell &cell)
{
x += cell.x;
y += cell.y;
return *this;
}
Cell &operator *(const Cell &cell)
{
x *= cell.x;
y *= cell.y;
return *this;
}
Cell &operator ++()
{
x += 1;
y += 1;
return *this;
}
bool operator <(const Cell &cell) const
{
if (x == cell.x)
{
return y < cell.y;
}
else
{
return x < cell.x;
}
}
bool operator >(const Cell &cell) const
{
if (x == cell.x)
{
return y > cell.y;
}
else
{
return x > cell.x;
}
}
bool operator ==(const Cell &cell) const
{
return x == cell.x && y == cell.y;
}
};
std::ostream &operator<<(std::ostream &os, const Cell &cell)
{
os << "{" << cell.x << "," << cell.y << "}";
return os;
}
int main()
{
auto generate = []()
{
int n = std::rand() % 10 + 100;
Cell cell{n, n};
return cell;
};
std::set<Cell> set1;
for (size_t index = 0; index < 5; index++)
{
//插入以给定的 args 原位构造的新元素到容器。
set1.emplace(std::rand() % 10 + 100, std::rand() % 10 + 100);
std::cout << "set1: ";
std::copy(set1.begin(), set1.end(), std::ostream_iterator<Cell>(std::cout, " "));
std::cout << std::endl;
}
std::cout << std::endl;
std::set<Cell> set2;
for (size_t index = 0; index < 5; index++)
{
//插入新元素到尽可能靠近恰在 hint 前的位置。原位构造元素,即不进行复制或移动操作。
set2.emplace_hint(set2.cbegin(), std::rand() % 10 + 100, std::rand() % 10 + 100);
std::cout << "set2: ";
std::copy(set2.begin(), set2.end(), std::ostream_iterator<Cell>(std::cout, " "));
std::cout << std::endl;
}
std::cout << std::endl;
std::cout << "swap before:" << std::endl;
std::set<Cell> set3{generate(), generate(), generate(), generate(), generate()};
std::cout << "set3: ";
std::copy(set3.begin(), set3.end(), std::ostream_iterator<Cell>(std::cout, " "));
std::cout << std::endl;
std::set<Cell> set4{generate(), generate(), generate(), generate(), generate()};
std::cout << "set4: ";
std::copy(set4.begin(), set4.end(), std::ostream_iterator<Cell>(std::cout, " "));
std::cout << std::endl;
//将内容与 other 的交换。不在单个元素上调用任何移动、复制或交换操作。
set3.swap(set4);
std::cout << "swap after:" << std::endl;
std::cout << "set3: ";
std::copy(set3.begin(), set3.end(), std::ostream_iterator<Cell>(std::cout, " "));
std::cout << std::endl;
std::cout << "set4: ";
std::copy(set4.begin(), set4.end(), std::ostream_iterator<Cell>(std::cout, " "));
std::cout << std::endl;
return 0;
}