std::set<Key,Compare,Allocator>::set
提供: cppreference.com
set(); explicit set( const Compare& comp, {{#pad:|12}} const Allocator& alloc = Allocator() ); |
||
explicit set( const Allocator& alloc ); |
(1) | (C++11以上) |
| (2) | ||
template< class InputIt > set( InputIt first, InputIt last, {{#pad:|3}} const Compare& comp = Compare(), {{#pad:|3}} const Allocator& alloc = Allocator() ); |
||
template< class InputIt > set( InputIt first, InputIt last, const Allocator& alloc) {{#pad:|3}} : 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, {{#pad:|3}} const Compare& comp = Compare(), {{#pad:|3}} const Allocator& alloc = Allocator() ); |
(C++11以上) | |
set( std::initializer_list<value_type> init, const Allocator& alloc ) {{#pad:|3}} : 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) 初期化子リストコンストラクタ。 初期化子リスト
init の内容を持つコンテナを拘置行します。 指定範囲内の複数の要素が等しいキーを持っている場合、どの要素が挿入されるかは未規定です (未解決の LWG2844)。引数
| alloc | - | このコンテナのすべてのメモリ確保のために使用するアロケータ |
| comp | - | キーのすべての比較のために使用する比較関数オブジェクト |
| first, last | - | 要素をコピーする範囲 |
| other | - | コンテナの要素を初期化するためのソースとして使用される別のコンテナ |
| init | - | コンテナの要素を初期化するための初期化子リスト |
| 型の要件 | ||
-InputIt は LegacyInputIterator の要件を満たさなければなりません。
| ||
-Compare は Compare の要件を満たさなければなりません。
| ||
-Allocator は Allocator の要件を満たさなければなりません。
| ||
計算量
1) 一定。
2) 一般的なケースでは N log(N)、ただし
N = std::distance(first, last)。 指定範囲が value_comp() によってすでにソートされている場合は N に比例。3)
other のサイズに比例。4) 一定。
alloc が指定されていて alloc != 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 で検討されています。
例
Run this code
#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) Default constructor
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) Iterator constructor
std::set<std::string> b(a.find("dog"), a.end());
for(auto& str: b) std::cout << str << ' ';
std::cout << '\n';
// (3) Copy constructor
std::set<std::string> c(a);
c.insert("another horse");
for(auto& str: c) std::cout << str << ' ';
std::cout << '\n';
// (4) Move constructor
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 constructor
std::set<std::string> e {"one", "two", "three", "five", "eight"};
for(auto& str: e) std::cout << str << ' ';
std::cout << '\n';
// custom comparison
std::set<Point, PointCmp> z = {{2, 5}, {3, 4}, {1, 1}};
z.insert({1, -1}); // this fails because the magnitude of 1,-1 equals 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 | the default constructor is explicit | made non-explicit |
関連項目
| コンテナに値を代入します (パブリックメンバ関数) |