std::set<Key,Compare,Allocator>:: set
| (1) | ||
|
set
(
)
;
|
(C++11まで) | |
|
set
(
)
:
set
(
Compare
(
)
)
{
}
|
(C++11から)
(constexprはC++26から) |
|
|
explicit
set
(
const
Compare
&
comp,
const Allocator & alloc = Allocator ( ) ) ; |
(2) | (C++26以降constexpr) |
|
explicit
set
(
const
Allocator
&
alloc
)
;
|
(3) |
(C++11以降)
(C++26以降constexpr) |
|
template
<
class
InputIt
>
set
(
InputIt first, InputIt last,
|
(4) | (C++26以降 constexpr) |
|
template
<
class
InputIt
>
set
(
InputIt first, InputIt last,
|
(5) |
(C++14以降)
(C++26以降constexpr) |
|
set
(
const
set
&
other
)
;
|
(6) | (C++26以降 constexpr) |
|
set
(
const
set
&
other,
const
Allocator
&
alloc
)
;
|
(7) |
(C++11以降)
(C++26以降 constexpr) |
|
set
(
set
&&
other
)
;
|
(8) |
(C++11以降)
(C++26以降constexpr) |
|
set
(
set
&&
other,
const
Allocator
&
alloc
)
;
|
(9) |
(C++11以降)
(C++26以降 constexpr) |
|
set
(
std::
initializer_list
<
value_type
>
init,
const
Compare
&
comp
=
Compare
(
)
,
|
(10) |
(C++11以降)
(C++26以降 constexpr) |
|
set
(
std::
initializer_list
<
value_type
>
init,
const
Allocator
&
alloc
)
|
(11) |
(C++14以降)
(C++26以降constexpr) |
|
template
<
container-compatible-range
<
value_type
>
R
>
set
(
std::
from_range_t
, R
&&
rg,
|
(12) |
(C++23以降)
(C++26以降 constexpr) |
|
template
<
container-compatible-range
<
value_type
>
R
>
set
(
std::
from_range_t
, R
&&
rg,
|
(13) |
(C++23以降)
(C++26以降 constexpr) |
様々なデータソースから新しいコンテナを構築し、オプションでユーザー提供のアロケータ alloc または比較関数オブジェクト comp を使用します。
[
first
,
last
)
。
|
alloc
が提供されない場合、アロケータは
std::
allocator_traits
<
allocator_type
>
::
|
(C++11以降) |
|
クラステンプレート引数推論
中、最初の引数のみがコンテナの
|
(C++23以降) |
|
クラステンプレート引数推論
の間、最初の引数のみがコンテナの
|
(C++23以降) |
目次パラメータ
計算量
1-3)
定数。
4,5)
N·log(N)
ここで
N
は
std::
distance
(
first, last
)
である。一般的には
N
に対して線形時間だが、
[
first
,
last
)
が既に
value_comp
(
)
によってソートされている場合は
N
に対して線形時間となる。
6,7)
サイズに対して線形
other
。
8,9)
定数時間。もし
alloc
が与えられ、かつ
alloc
!
=
other.
get_allocator
(
)
の場合、線形時間。
10,11)
N·log(N)
ここで
N
は
init.
size
(
)
一般には
N
に対して線形時間、
init
が既に
value_comp
(
)
でソートされている場合は
N
に対して線形時間。
12,13)
N·log(N)
ここで
N
は
ranges::
distance
(
rg
)
です。一般的には
N
に対して線形時間ですが、
rg
が既に
value_comp
(
)
でソートされている場合は
N
に対して線形時間となります。
例外
注記コンテナのムーブ構築後(オーバーロード (8,9))、元々 C++23まで正式に要求されていないものの、一部の実装は既により以前のモードでテンプレートパラメータ
例
このコードを実行
#include <cmath> #include <iostream> #include <set> #include <string> 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); } }; std::ostream& operator<<(std::ostream& os, Point pt) { return os << '(' << pt.x << ',' << pt.x << ')'; } void println(auto rem, const auto& seq) { std::cout << rem << '{'; for (auto n{seq.size()}; const auto& elm : seq) std::cout << elm << (--n ? ", " : ""); std::cout << "}\n"; } int main() { // (1) デフォルトコンストラクタ std::set<std::string> a; a.insert("horse"); a.insert("cat"); a.insert("dog"); println("1) a: ", a); // (4) 範囲コンストラクタ std::set<std::string> b(a.find("dog"), a.end()); println("2) b: ", b); // (6) コピーコンストラクタ std::set<std::string> c(a); c.insert("another horse"); println("3) c: ", c); // (8) ムーブコンストラクタ std::set<std::string> d(std::move(a)); println("4) d: ", d); println("5) a: ", a); // (10) 初期化子リストコンストラクタ std::set<std::string> e{"one", "two", "three", "five", "eight"}; println("6) e: ", e); // カスタム比較関数 std::set<Point, PointCmp> f = {{2, 5}, {3, 4}, {1, 1}}; f.insert({1, -1}); // これは失敗します - (1,-1)の大きさが(1,1)と等しいため println("7) f: ", f); // (12) 範囲コンストラクタ const auto w = {"Eurybia", "Theia", "Rhea", "Aura", "Mnemosyne", "Mnemosyne"}; #if __cpp_lib_containers_ranges std::set<std::string> g(std::from_range, w); // オーバーロード (12) #else std::set<std::string> g(w.begin(), w.end()); // (4)へのフォールバック #endif println("8) g: ", g); } 出力例: 1) a: {cat, dog, horse}
2) b: {dog, horse}
3) c: {another horse, cat, dog, horse}
4) d: {cat, dog, horse}
5) a: {}
6) e: {eight, five, one, three, two}
7) f: {(1,1), (3,3), (2,2)}
8) g: {Aura, Eurybia, Mnemosyne, Rhea, Theia}
欠陥報告以下の動作変更の欠陥報告書は、以前に公開されたC++規格に対して遡及的に適用されました。
関連項目
| ||||||||||||||||||||||||||||||||||||||||||||||||||||