CppDS.com

C++ 98 11 14 17 20 手册

std::set<Key,Compare,Allocator>::set

来自cppreference.com
< cpp‎ | container‎ | set

set();

explicit set( const Compare& comp,

              const Allocator& alloc = Allocator() );
explicit set( const Allocator& alloc );
(1) (C++11 起)
(2)
template< class InputIt >

set( InputIt first, InputIt last,
     const Compare& comp = Compare(),

     const Allocator& alloc = Allocator() );
template< class InputIt >

set( InputIt first, InputIt last, const Allocator& alloc)

    : set(first, last, Compare(), alloc) {}
(C++14 起)
set( const set& other );
(3)
set( const set& other, const Allocator& alloc );
(3) (C++11 起)
set( set&& other );
(4) (C++11 起)
set( set&& other, const Allocator& alloc );
(4) (C++11 起)
(5)
set( std::initializer_list<value_type> init,

     const Compare& comp = Compare(),

     const Allocator& alloc = Allocator() );
(C++11 起)
set( std::initializer_list<value_type> init, const Allocator& alloc )
    : set(init, Compare(), alloc) {}
(C++14 起)

从各种数据源,可选地用用户提供的分配器 alloc 或比较函数对象 comp 构造新容器。

1) 默认构造函数。构造新容器。
2) 范围构造函数。构造拥有范围 [first, last) 内容的容器。若范围中的多个元素拥有比较等价的关键,则插入哪个元素是未指定的(待决的 LWG2844 )。
3) 复制构造函数。以 other 内容的副本构造容器。若不提供 alloc ,则通过调用 std::allocator_traits<allocator_type>::select_on_container_copy_construction(other.get_allocator()) 获得分配器。
4) 移动构造函数。用移动语义构造拥有 other 内容的容器。若不提供 alloc ,则以从属于 other 的分配器移动构造获得分配器。
5) initializer_list 构造函数。构造拥有 initializer_list init 内容的容器。若范围中的多个元素拥有比较等价的关键,则插入哪个元素是未指定的(待决的 LWG2844 )。

参数

alloc - 用于此容器所有内存分配的分配器
comp - 用于所有关键比较的比较函数对象
first, last - 复制元素的来源范围
other - 将用作初始化容器元素所用源的另一容器
init - 初始化容器元素所用的 initializer_list
类型要求
-
InputIt 必须满足遗留输入迭代器 (LegacyInputIterator) 的要求。
-
Compare 必须满足比较 (Compare) 的要求。
-
Allocator 必须满足分配器 (Allocator) 的要求。

复杂度

1) 常数
2) 通常为 N log(N) ,若范围已经由 value_comp() 排序则与 N 成线性,其中 N = std::distance(first, last)
3)other 的大小成线性。
4) 常数。若给定 allocalloc != other.get_allocator() ,则为线性。
5) N log(N) ,其中通常有 N = init.size()) 。若 init 已按照 value_comp() 排序则与 N 成线性。

异常

调用 Allocator::allocate 可能抛出。

注意

在容器移动构造(重载 (4) )后,指向 other 的引用及迭代器(除了尾迭代器)保持合法,但指代现于 *this 中的元素。当前标准由 [container.requirements.general]/12 中的总括陈述作出此保证,而 LWG 2321 正在考虑更严格的保证。

示例

#include <iostream>
#include <string>
#include <set>
#include <cmath>
 
struct Point { double x, y; };
struct PointCmp {
    bool operator()(const Point& lhs, const Point& rhs) const { 
        return std::hypot(lhs.x, lhs.y) < std::hypot(rhs.x, rhs.y); 
    }
};
 
int main()
{
  // (1) 默认初始化器
  std::set<std::string> a;
  a.insert("cat");
  a.insert("dog");
  a.insert("horse");
  for(auto& str: a) std::cout << str << ' ';
  std::cout << '\n';
 
  // (2) 迭代器初始化器
  std::set<std::string> b(a.find("dog"), a.end());
  for(auto& str: b) std::cout << str << ' ';
  std::cout << '\n';
 
  // (3) 复制构造函数
  std::set<std::string> c(a);
  c.insert("another horse");
  for(auto& str: c) std::cout << str << ' ';
  std::cout << '\n';
 
  // (4) 移动构造函数
  std::set<std::string> d(std::move(a));
  for(auto& str: d) std::cout << str << ' ';
  std::cout << '\n';
  std::cout << "moved-from set is ";
  for(auto& str: a) std::cout << str << ' ';
  std::cout << '\n';
 
  // (5) initializer_list 构造函数
  std::set<std::string> e {"one", "two", "three", "five", "eight"};
  for(auto& str: e) std::cout << str << ' ';
  std::cout << '\n';
 
  // 自定义比较
  std::set<Point, PointCmp> z = {{2, 5}, {3, 4}, {1, 1}};
  z.insert({1, -1}); // 这会失败,因为 1,-1 的长度等于 1,1
  for(auto& p: z) std::cout << '(' << p.x << ',' << p.y << ") ";
  std::cout << '\n';
}

输出:

cat dog horse 
dog horse 
another horse cat dog horse 
cat dog horse 
moved-from set is 
eight five one three two 
(1,1) (3,4) (2,5)

缺陷报告

下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。

DR 应用于 出版时的行为 正确行为
LWG 2193 C++11 默认构造函数为 explicit 使之为非 explicit

参阅

赋值给容器
(公开成员函数)
关闭