名前空間
変種
操作

std::set

From cppreference.com
< cpp‎ | container
 
 
 
 
ヘッダー <set> で定義
template<

    class Key,
    class Compare = std::less<Key>,
    class Allocator = std::allocator<Key>

> class set;
(1)
namespace pmr {

    template<
        class Key,
        class Compare = std::less<Key>
    > using set = std::set<Key, Compare, std::pmr::polymorphic_allocator<Key>>;

}
(2) (C++17以降)

std::setは、Key型のユニークなオブジェクトのソート済み集合を格納する連想コンテナである。ソートはキー比較関数Compareを用いて行われる。検索、削除、挿入の操作は対数計算量を持つ。setは通常、赤黒木として実装される。

標準ライブラリがCompare要件を使用するあらゆる場所で、一意性(uniqueness)は同値関係(equivalence relation)を用いて決定される。厳密ではない言い方をすれば、2つのオブジェクトabは、どちらも他方より小さいと見なされない場合、つまり!comp(a, b) && !comp(b, a)の場合に同値であると見なされる。

std::setは、ContainerAllocatorAwareContainerAssociativeContainer、そしてReversibleContainerの要件を満たす。

std::setの全てのメンバ関数はconstexprである:定数式の評価においてstd::setオブジェクトを作成し、使用することが可能である。

しかし、動的に割り当てられたストレージは同じ定数式の評価内で解放されなければならないため、std::setオブジェクトは一般的にconstexprにはなれない。

(C++26以降)

目次

[編集] テンプレートパラメータ

[編集] メンバ型

定義
key_type Key[編集]
value_type Key[編集]
size_type 符号なし整数型 (通常は std::size_t)[編集]
difference_type 符号付き整数型 (通常は std::ptrdiff_t)[編集]
key_compare Compare[編集]
value_compare Compare[編集]
allocator_type Allocator[編集]
reference value_type&[編集]
const_reference const value_type&[編集]
pointer

Allocator::pointer

(C++11まで)

std::allocator_traits<Allocator>::pointer

(C++11以降)
[編集]
const_pointer

Allocator::const_pointer

(C++11まで)

std::allocator_traits<Allocator>::const_pointer

(C++11以降)
[編集]
iterator value_typeへの定数LegacyBidirectionalIterator かつConstexprIterator(C++26以降)[編集]
const_iterator const value_type への LegacyBidirectionalIterator および ConstexprIterator(C++26以降)[編集]
reverse_iterator std::reverse_iterator<iterator>[編集]
const_reverse_iterator std::reverse_iterator<const_iterator>[編集]
node_type (C++17以降) コンテナのノードを表す ノードハンドル の特殊化[編集]
insert_return_type (C++17 以降) node_type の挿入結果を記述する型。以下の特殊化である

template<class Iter, class NodeType>
struct /*未規定*/
{
    Iter     position;
    bool     inserted;
    NodeType node;
};

テンプレート引数 iteratornode_type でインスタンス化される。[編集]

[編集] メンバ関数

setを構築する
(公開メンバ関数) [編集]
setを破棄する
(公開メンバ関数) [編集]
コンテナに値を代入する
(公開メンバ関数) [編集]
関連付けられたアロケータを返す
(公開メンバ関数) [編集]
イテレータ
先頭へのイテレータを返す
(public メンバ関数) [編集]
(C++11)
末尾へのイテレータを返す
(public メンバ関数) [編集]
先頭への逆イテレータを返す
(public メンバ関数) [編集]
(C++11)
末尾への逆イテレータを返す
(public メンバ関数) [編集]
容量
コンテナが空かどうかをチェックする
(public メンバ関数) [編集]
要素数を返す
(public メンバ関数) [編集]
可能な最大要素数を返す
(public メンバ関数) [編集]
変更
内容をクリアする
(公開メンバ関数) [編集]
要素 またはノード(C++17以降)を挿入する
(公開メンバ関数) [編集]
要素の範囲を挿入する
(公開メンバ関数) [編集]
(C++11)
要素を直接構築する
(公開メンバ関数) [編集]
ヒントを使用して要素を直接構築する
(公開メンバ関数) [編集]
要素を削除する
(公開メンバ関数) [編集]
内容を交換する
(public メンバ関数) [編集]
(C++17)
コンテナからノードを抽出する
(公開メンバ関数) [編集]
(C++17)
他のコンテナからノードを連結する
(公開メンバ関数) [編集]
検索
特定のキーに一致する要素の数を返す
(公開メンバ関数) [編集]
特定のキーを持つ要素を検索する
(公開メンバ関数) [編集]
(C++20)
コンテナが特定のキーを持つ要素を含むか確認する
(公開メンバ関数) [編集]
特定のキーに一致する要素の範囲を返す
(公開メンバ関数) [編集]
指定されたキーより小さくない最初の要素へのイテレータを返す
(公開メンバ関数) [編集]
指定されたキーより大きい最初の要素へのイテレータを返す
(公開メンバ関数) [編集]
監視
キーを比較する関数を返す
(公開メンバ関数) [編集]
value_type 型のオブジェクト内のキーを比較する関数を返す
(公開メンバ関数) [編集]

[編集] 非メンバ関数

(C++20で削除)(C++20で削除)(C++20で削除)(C++20で削除)(C++20で削除)(C++20)
2つのsetの値を辞書順で比較する
(関数テンプレート) [編集]
std::swap アルゴリズムを特殊化する
(関数テンプレート) [編集]
特定の基準を満たすすべての要素を削除する
(関数テンプレート) [編集]

推論補助

(C++17以降)

[編集] ノート

メンバ型 iteratorconst_iterator は同じ型へのエイリアスかもしれない。これは、これら2つの型をパラメータ型として使用する関数オーバーロードのペアを定義すると、ODR (One Definition Rule) に違反する可能性があることを意味する。iteratorconst_iterator に変換可能なので、代わりに const_iterator をパラメータ型として持つ単一の関数が機能する。

機能テストマクロ 規格 機能
__cpp_lib_containers_ranges 202202L (C++23) コンテナのRangeコンストラクタとRange挿入
__cpp_lib_constexpr_containers 202502L (C++26) constexpr std::set

[編集]

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <set>
#include <string_view>
 
template<typename T>
std::ostream& operator<<(std::ostream& out, const std::set<T>& set)
{
    if (set.empty())
        return out << "{}";
    out << "{ " << *set.begin();
    std::for_each(std::next(set.begin()), set.end(), [&out](const T& element)
    {
        out << ", " << element;
    });
    return out << " }";
}
 
int main()
{
    std::set<int> set{1, 5, 3};
    std::cout << set << '\n';
 
    set.insert(2);
    std::cout << set << '\n';
 
    set.erase(1);
    std::cout << set << "\n\n";
 
    std::set<int> keys{3, 4};
    for (int key : keys)
    {
        if (set.contains(key))
            std::cout << set << " does contain " << key << '\n';
        else
            std::cout << set << " doesn't contain " << key << '\n';
    }
    std::cout << '\n';
 
    std::string_view word = "element";
    std::set<char> characters(word.begin(), word.end());
    std::cout << "There are " << characters.size() << " unique characters in "
              << std::quoted(word) << ":\n" << characters << '\n';
}

出力

{ 1, 3, 5 }
{ 1, 2, 3, 5 }
{ 2, 3, 5 }
 
{ 2, 3, 5 } does contain 3
{ 2, 3, 5 } doesn't contain 4
 
There are 5 unique characters in "element":
{ e, l, m, n, t }

[編集] 欠陥報告

以下の動作変更を伴う欠陥報告が、以前に公開されたC++標準に遡って適用されました。

DR 適用対象 公開された動作 正しい動作
LWG 103 C++98 イテレータがキーの変更を許可していた イテレータが定数になった
LWG 230 C++98 KeyCopyConstructible であることを要求されていなかった
(Key 型のキーが構築できない可能性があった)
Key には次のことも要求される
CopyConstructible であること

[編集] 関連項目

キーによってソートされたキーのコレクション
(クラステンプレート) [編集]
キーによってハッシュ化されたユニークなキーのコレクション
(クラステンプレート) [編集]
(C++23)
ユニークなキーのコレクションを提供するためにコンテナを適応させる、キーによってソートされる
(class template) [編集]
English 日本語 中文(简体) 中文(繁體)