set クラス

コレクションのデータを格納および取得するには、C++ 標準ライブラリ コンテナー クラス set を使用します。 set 内の要素の値は一意であり、データの自動的な順序付けに従ってキー値として機能します。 set 内の要素の値は直接変更できません。 代わりに、以前の値を削除し、新しい値の要素を挿入する必要があります。

構文

template <class Key,
    class Traits=less<Key>,
    class Allocator=allocator<Key>>
class set

パラメーター

Key
set に格納される要素のデータ型。

Traits
2 つの要素の値を並べ替えキーとして比較して、set 内の要素の相対順序を決定できる関数オブジェクトを提供する型。 この引数は省略可能であり、既定値は二項述語 less <Key> です。

C++ 14 では、型パラメーターを使用せずに std::less<> 述語または std::greater<> 述語を指定することで、異種ルックアップを有効にすることができます。 詳細については、「連想コンテナーの異種ルックアップ」をご覧ください。

Allocator
メモリの set の割り当てと解放に関する詳細をカプセル化する、格納されたアロケーター オブジェクトを表す型。 この引数は省略可能であり、既定値は allocator<Key> です。

解説

C++ 標準ライブラリの set の概要

  • hash_map は連想コンテナーであり、関連付けられたキー値に基づいて要素の値を効率的に取得できるようにする可変サイズのコンテナーとして機能します。 また単純な連想コンテナーでもあります。これは、要素値がキー値であるためです。

  • 反転することができます。これは、hash_map には、要素にアクセスするための双方向反復子が用意されているためです。

  • 並べ替えが実行されます。これは、指定した比較関数に従ってコンテナー内のキー値によって要素に順序が設定されるためです。

  • 一意のクラスです。これは、各要素が一意のキーを持つ必要があるためです。 set は、単純な連想コンテナーであるため、要素も一意です。

set はクラス テンプレートとしても表されます。これは、このクラスに用意されている機能が汎用的な機能であり、要素またはキーとして保持されているデータの特定の型に依存しないためです。 使用されているデータ型は、クラス テンプレートで比較関数やアロケーターと共にパラメーターとして指定されます。

一般的に、コンテナー型の選択は、アプリケーションにおいて必要な検索および挿入の種類に基づいている必要があります。 連想コンテナーは、検索、挿入、削除の各操作用に最適化されています。 これらの操作を明示的にサポートするメンバー関数は効率的であり、処理時間は平均的にコンテナー内にある要素の数の対数に比例します。 要素を挿入しても反復子の有効性は失われません。また、要素を削除した場合は、削除された要素を指す反復子だけが無効化されます。

値とキーを関連付ける条件をアプリケーションが満たしている場合、set は最適な連想コンテナーとなっている必要があります。 set の要素は一意であり、独自の並べ替えキーとして機能します。 この種類の構造体のモデルは、単語が 1 回だけ出現する可能性がある単語の順序付きのリストです。 キーワードを複数設定できる場合は、multiset が適切なコンテナー構造体となります。 値が一意のキーワードのリストにアタッチされている必要がある場合、map がこのデータを格納するのに適切な構造体です。 キーが一意でない場合は、multimap が最適なコンテナーです。

set では、key_compare 型の格納されている関数オブジェクトを呼び出すことで、制御するシーケンスを並べ替えます。 この格納されているオブジェクトは比較関数であり、メンバー関数 key_comp を呼び出してアクセスできます。 通常、要素は、この順序を確立するために小なり比較だけを実行できる必要があります。これにより、2 つの要素が指定されたときに、それらの要素が等しいか (どちらか一方が小さくはない)、または一方が他方より小さいかを判断できます。 この結果、等価でない複数の要素間で順序が付けられます。 テクニカル ノートでは、比較関数は、数学上の標準的な意味で厳密弱順序を発生させる二項述語であると示されています。 二項述語 f(x,y) は、2 つの引数オブジェクト x および y と戻り値 true または false を持つ関数オブジェクトです。 set に適用される順序付けは、二項述語が非再帰、反対称、推移的であり、等価性が推移的である (2 つのオブジェクト xy が、fx,y) と f(y,x) の両方が false の場合に等価になるように定義されている) 場合、厳密弱順序になります。 2 つのキーの等値に関する条件が等価性の条件よりも厳しく、優先される場合、順序付けは完全な順序付け (すべての要素が相互の値に基づいて並べ替えられる) となり、一致するそれぞれのキーを識別するのが難しくなります。

C++ 14 では、型パラメーターを使用せずに std::less<> 述語または std::greater<> 述語を指定することで、異種ルックアップを有効にすることができます。 詳細については、「連想コンテナーの異種ルックアップ」をご覧ください。

set クラスに用意されている反復子は双方向反復子ですが、クラス メンバー関数 insertset には、弱い入力反復子をテンプレート パラメーターとして取得するバージョンがあります。この反復子の機能に関する要件は、双方向反復子のクラスで保証されている要件よりも低くなっています。 これらの反復子の機能に差異があるのは、反復子の概念が異なっているためです。 反復子の各概念には、反復子独自の一連の要件が含まれています。また、それらの要件を使用するアルゴリズムでは、反復子の種類ごとに指定されている要件に対して、前提を絞り込む必要があります。 たとえば、一部のオブジェクトを参照するために入力反復子が逆参照される可能性があることを前提とする場合があります。さらに、シーケンス内にある次の反復子に対して逆参照が増加する可能性があることを前提とする場合もあります。 このことは、最小限実施することですが、クラスのメンバー関数のコンテキストに含まれる反復子の範囲 [ First, Last) について明確にすることも重要です。

コンストラクター

名前 説明
set 空の set を構築するか、他の set の全体または一部のコピーである set を構築します。

Typedefs

名前 説明
allocator_type set オブジェクトの allocator クラスを表す型。
const_iterator set 内の const 要素を読み取ることができる双方向反復子を提供する型。
const_pointer set 内の const 要素へのポインターを提供する型。
const_reference 読み取りと const 操作を実行するために、set に格納された const 要素への参照を提供する型。
const_reverse_iterator set 内の任意の const 要素を読み取ることができる双方向反復子を提供する型。
difference_type set の要素の数を、反復子が指す要素の範囲に基づいて表すために使用できる符号付き整数型。
iterator set 内の任意の要素の読み取りまたは変更ができる双方向反復子を提供する型。
key_compare 2 つの並べ替えキーを比較して、set 内の 2 つの要素の相対順序を決定できる関数オブジェクトを提供する型。
key_type この型は、並べ替えキーとしてキャパシティ内で set の要素として格納されるオブジェクトを表します。
pointer set 内の要素へのポインターを提供する型。
reference set に格納されている要素への参照を提供する型。
reverse_iterator 反転された set 内の 1 つの要素の読み取りまたは変更ができる双方向反復子を提供する型。
size_type set 内の要素の数を表すことができる符号なし整数型。
value_compare 2 つの要素を比較して、set 内の要素の相対順序を決定できる関数オブジェクトを提供する型。
value_type この型は、値としてキャパシティ内で set の要素として格納されるオブジェクトを表します。

関数

名前 説明
begin set 内の最初の要素を指す反復子を返します。
cbegin set 内の最初の要素を指す定数反復子を返します。
cend set 内の最後の要素の次の位置を指す定数反復子を返します。
clear set のすべての要素を消去します。
containsC++20 set 内に指定されたキーを持つ要素があるかどうかを確認します。
count パラメーター指定したキーに一致するキーを持つ、set 内の要素の数を返します。
crbegin 反転された set 内の最初の要素を指す定数反復子を返します。
crend 反転された set 内の最後の要素の次の位置を指す定数反復子を返します。
emplace インプレースで構築された要素を set に挿入します。
emplace_hint インプレースで構築された要素を、配置ヒントと共に set に挿入します。
empty set が空かどうかをテストします。
end set 内の最後の要素の次の位置を指す反復子を返します。
equal_range 指定したキーよりも大きいキーを持つ、set 内の最初の要素を指す反復子と、およびそのキー以上のキーを持つ、set 内の最初の要素を指す反復子のペアを返します。
erase セット内の要素または要素の範囲を指定した位置から削除するか、または指定したキーと一致する要素を削除します。
find 指定したキーと同じキーを持つ、set 内の要素の位置を指す反復子を返します。
get_allocator allocator の構築に使用される set オブジェクトのコピーを返します。
insert set に要素または要素範囲を挿入します。
key_comp set 内のキーを並べ替えるために使用される比較オブジェクトのコピーを取得します。
lower_bound 指定したキー以上のキーを持つ、set 内の最初の要素を指す反復子を返します。
max_size set の最大長を返します。
rbegin 反転された set 内の最初の要素を指す反復子を返します。
rend 反転された set 内の最後の要素の次の位置を指す反復子を返します。
size set 内の要素数を返します。
swap 2 つの set の要素を交換します。
upper_bound 指定したキー以上のキーを持つ、set 内の最初の要素を指す反復子を返します。
value_comp set 内の要素の値を並べ替えるために使用される比較オブジェクトのコピーを取得します。

演算子

名前 説明
operator= 別の set のコピーで set の要素を置き換えます。

allocator_type

set オブジェクトのアロケータ― クラスを表す型です。

typedef Allocator allocator_type;

解説

allocator_type は、テンプレート パラメーター Allocator のシノニムです。

multiset が要素の並べ替えに使用する関数オブジェクトを返します。テンプレート パラメーター Allocator です。

Allocator の詳細については、set クラスのトピックのコメントのセクションをご覧ください。

allocator_type の使用例については、get_allocator の例をご覧ください。

begin

set 内の最初の要素を指す定数反復子を返します。

const_iterator begin() const;

iterator begin();

戻り値

set 内の最初の要素、または空の set の次の位置を指す双方向反復子。

解説

begin の戻り値が const_iterator に割り当てられている場合、set オブジェクト内の要素は変更できません。 begin の戻り値が iterator に割り当てられている場合、set オブジェクト内の要素は変更できます。

// set_begin.cpp
// compile with: /EHsc
#include <set>
#include <iostream>

int main( )
{
   using namespace std;
   set <int> s1;
   set <int>::iterator s1_Iter;
   set <int>::const_iterator s1_cIter;

   s1.insert( 1 );
   s1.insert( 2 );
   s1.insert( 3 );

   s1_Iter = s1.begin( );
   cout << "The first element of s1 is " << *s1_Iter << endl;

   s1_Iter = s1.begin( );
   s1.erase( s1_Iter );

   // The following 2 lines would err because the iterator is const
   // s1_cIter = s1.begin( );
   // s1.erase( s1_cIter );

   s1_cIter = s1.begin( );
   cout << "The first element of s1 is now " << *s1_cIter << endl;
}
The first element of s1 is 1
The first element of s1 is now 2

cbegin

範囲内の最初の要素を示す const 反復子を返します。

const_iterator cbegin() const;

戻り値

範囲の最初の要素、または空の範囲の末尾の次の位置 (空の範囲の場合、const) を指し示す cbegin() == cend() 双方向アクセス反復子。

解説

cbegin の戻り値で範囲内の要素を変更することはできません。

begin() メンバー関数の代わりにこのメンバー関数を使用して、戻り値が const_iterator になることを保証できます。 通常は、次の例に示すように auto 型推論キーワードと共に使用します。 例では、Containerconstbegin() をサポートする任意の種類の変更可能な (非 cbegin()) コンテナーであると見なします。

auto i1 = Container.begin();
// i1 is Container<T>::iterator
auto i2 = Container.cbegin();

// i2 is Container<T>::const_iterator

cend

範囲内の最後の要素の次の位置を指す const 反復子を返します。

const_iterator cend() const;

戻り値

範囲の末尾の次の位置を指し示す const 双方向アクセス反復子。

解説

cend は、反復子が範囲の末尾を超えたかどうかをテストするために使用されます。

end() メンバー関数の代わりにこのメンバー関数を使用して、戻り値が const_iterator になることを保証できます。 通常は、次の例に示すように auto 型推論キーワードと共に使用します。 例では、Containerconstend() をサポートする任意の種類の変更可能な (非 cend()) コンテナーであると見なします。

auto i1 = Container.end();
// i1 is Container<T>::iterator
auto i2 = Container.cend();

// i2 is Container<T>::const_iterator

cend によって返された値は逆参照しないでください。

clear

set のすべての要素を消去します。

void clear();

// set_clear.cpp
// compile with: /EHsc
#include <set>
#include <iostream>

int main( )
{
   using namespace std;
   set <int> s1;

   s1.insert( 1 );
   s1.insert( 2 );

   cout << "The size of the set is initially " << s1.size( )
        << "." << endl;

   s1.clear( );
   cout << "The size of the set after clearing is "
        << s1.size( ) << "." << endl;
}
The size of the set is initially 2.
The size of the set after clearing is 0.

const_iterator

set 内の const 要素を読み取ることができる双方向反復子を提供する型。

typedef implementation-defined const_iterator;

解説

const_iterator 型で要素の値を変更することはできません。

const_iterator の使用例については、begin の例をご覧ください。

const_pointer

set 内の const 要素へのポインターを提供する型。

typedef typename allocator_type::const_pointer const_pointer;

解説

const_pointer 型で要素の値を変更することはできません。

ほとんどの場合、const set オブジェクト内の要素にアクセスするには、const_iterator を使用する必要があります。

const_reference

読み取りと const 操作を実行するために、set に格納された const 要素への参照を提供する型。

typedef typename allocator_type::const_reference const_reference;

// set_const_ref.cpp
// compile with: /EHsc
#include <set>
#include <iostream>

int main( )
{
   using namespace std;
   set <int> s1;

   s1.insert( 10 );
   s1.insert( 20 );

   // Declare and initialize a const_reference &Ref1
   // to the 1st element
   const int &Ref1 = *s1.begin( );

   cout << "The first element in the set is "
        << Ref1 << "." << endl;

   // The following line would cause an error because the
   // const_reference can't be used to modify the set
   // Ref1 = Ref1 + 5;
}
The first element in the set is 10.

const_reverse_iterator

set 内の任意の const 要素を読み取ることができる双方向反復子を提供する型。

typedef std::reverse_iterator<const_iterator> const_reverse_iterator;

解説

const_reverse_iterator 型は要素の値を変更できず、逆の順序で set を反復処理するために使用します。

const_reverse_iterator の宣言や使用の方法の例については、rend の例を参照してください。

contains

set 内に指定されたキーを持つ要素があるかどうかを確認します。

bool contains(const Key& key) const;
template<class K> bool contains(const K& key) const;

パラメーター

K
キーの型。

key
探す要素のキー値。

戻り値

要素が set 内に見つかった場合は true、それ以外の場合は false です。

解説

contains() は C++20 の新機能です。 これを使用するには、/std:c++20 以降のコンパイラ オプションを指定します。

template<class K> bool contains(const K& key) const が透過的な場合にのみ、key_compare はオーバーロードの解決に使用されます。 詳細については、「連想コンテナーの異種ルックアップ」をご覧ください。

// Requires /std:c++20 or /std:c++latest
#include <set>
#include <iostream>

int main()
{
    std::set<int> theSet = {1, 2};

    std::cout << std::boolalpha; // so booleans show as 'true' or 'false'
    std::cout << theSet.contains(2) << '\n';
    std::cout << theSet.contains(3) << '\n';

    return 0;
}
true
false

count

パラメーター指定したキーに一致するキーを持つ、set 内の要素の数を返します。

size_type count(const Key& key) const;

パラメーター

key
照合される set の要素のキー。

戻り値

並べ替えキーがパラメーター キーと一致する要素が set に含まれている場合は 1。 set に一致するキーを持つ要素が含まれていない場合は 0。

解説

メンバー関数は、次の範囲内の要素の数を返します。

[ lower_bound(key), upper_bound(key) )。

set::count メンバー関数の使用例を次に示します。

// set_count.cpp
// compile with: /EHsc
#include <set>
#include <iostream>

int main()
{
    using namespace std;
    set<int> s1;
    set<int>::size_type i;

    s1.insert(1);
    s1.insert(1);

    // Keys must be unique in set, so duplicates are ignored
    i = s1.count(1);
    cout << "The number of elements in s1 with a sort key of 1 is: "
         << i << "." << endl;

    i = s1.count(2);
    cout << "The number of elements in s1 with a sort key of 2 is: "
         << i << "." << endl;
}
The number of elements in s1 with a sort key of 1 is: 1.
The number of elements in s1 with a sort key of 2 is: 0.

crbegin

反転された set 内の最初の要素を指す定数反復子を返します。

const_reverse_iterator crbegin() const;

戻り値

反転された set 内の最初の要素を示す、または反転されていない set 内の最後の要素だったものを示す const 反転双方向反復子。

解説

crbegin は、begin が set で使用されるように、逆順の set で使用されます。

戻り値が crbegin の場合、set オブジェクトは変更できません。

// set_crbegin.cpp
// compile with: /EHsc
#include <set>
#include <iostream>

int main( )
{
   using namespace std;
   set <int> s1;
   set <int>::const_reverse_iterator s1_crIter;

   s1.insert( 10 );
   s1.insert( 20 );
   s1.insert( 30 );

   s1_crIter = s1.crbegin( );
   cout << "The first element in the reversed set is "
        << *s1_crIter << "." << endl;
}
The first element in the reversed set is 30.

crend

反転された set 内の最後の要素の次の位置を指す定数反復子を返します。

const_reverse_iterator crend() const;

戻り値

逆順の set 内の最後の要素の次の場所 (通常の順序の set 内の最初の要素の前の場所) を指す定数逆順双方向反復子。

解説

crend は、end が set で使用されるように、逆順の set で使用されます。

戻り値が crend の場合、set オブジェクトは変更できません。 crend によって返された値は逆参照しないでください。

crend を使用して、逆順反復子が set の末尾に達したかどうかをテストできます。

// set_crend.cpp
// compile with: /EHsc
#include <set>
#include <iostream>

int main() {
   using namespace std;
   set <int> s1;
   set <int>::const_reverse_iterator s1_crIter;

   s1.insert( 10 );
   s1.insert( 20 );
   s1.insert( 30 );

   s1_crIter = s1.crend( );
   s1_crIter--;
   cout << "The last element in the reversed set is "
        << *s1_crIter << "." << endl;
}

difference_type

set の要素の数を、反復子が指す要素の範囲に基づいて表すために使用できる符号付き整数型。

typedef typename allocator_type::difference_type difference_type;

解説

difference_type は、コンテナーの反復子を減算またはインクリメントするときに返される型です。 通常 difference_type は、[ first, last) の範囲内で、反復子 firstlast の間にある要素の数を表すために使用され、first が指す要素と、last が指す要素の 1 つ前までの範囲の要素を含みます。

difference_type は、入力反復子の要件を満たすすべての反復子 (set などの反転可能なコンテナーによってサポートされる双方向反復子のクラスを含む) に対して使用できますが、反復子間の減算は、vector などのランダム アクセス コンテナーによって提供される、ランダム アクセス反復子によってのみサポートされます。

// set_diff_type.cpp
// compile with: /EHsc
#include <iostream>
#include <set>
#include <algorithm>

int main( )
{
   using namespace std;

   set <int> s1;
   set <int>::iterator s1_Iter, s1_bIter, s1_eIter;

   s1.insert( 20 );
   s1.insert( 10 );
   s1.insert( 20 );   // won't insert as set elements are unique

   s1_bIter = s1.begin( );
   s1_eIter = s1.end( );

   set <int>::difference_type   df_typ5, df_typ10, df_typ20;

   df_typ5 = count( s1_bIter, s1_eIter, 5 );
   df_typ10 = count( s1_bIter, s1_eIter, 10 );
   df_typ20 = count( s1_bIter, s1_eIter, 20 );

   // the keys, and hence the elements of a set are unique,
   // so there's at most one of a given value
   cout << "The number '5' occurs " << df_typ5
        << " times in set s1.\n";
   cout << "The number '10' occurs " << df_typ10
        << " times in set s1.\n";
   cout << "The number '20' occurs " << df_typ20
        << " times in set s1.\n";

   // count the number of elements in a set
   set <int>::difference_type  df_count = 0;
   s1_Iter = s1.begin( );
   while ( s1_Iter != s1_eIter)
   {
      df_count++;
      s1_Iter++;
   }

   cout << "The number of elements in the set s1 is: "
        << df_count << "." << endl;
}
The number '5' occurs 0 times in set s1.
The number '10' occurs 1 times in set s1.
The number '20' occurs 1 times in set s1.
The number of elements in the set s1 is: 2.

emplace

インプレースで構築された (コピーまたは移動操作が実行されない) 要素を挿入します。

template <class... Args>
pair<iterator, bool>
emplace(
    Args&&... args);

パラメーター

args
値が同じ順序付けになる要素がセットにまだ含まれていない場合に、セットに挿入される要素を構築するために転送される引数。

戻り値

pair。その bool コンポーネントは、挿入が行われた場合は true を返し、マップに既に等しい順序の値を持つ要素が含まれている場合は false を返します。 戻り値ペアの反復子コンポーネントは、新しい要素が挿入されるアドレス (bool コンポーネントが true の場合) または要素が既に配置されているアドレス (bool コンポーネントが false の場合) を返します。

解説

この関数では、反復子や参照は無効になりません。

配置の実行中、例外がスローされるとコンテナーの状態は変更されません。

// set_emplace.cpp
// compile with: /EHsc
#include <set>
#include <string>
#include <iostream>

using namespace std;

template <typename S> void print(const S& s) {
    cout << s.size() << " elements: ";

    for (const auto& p : s) {
        cout << "(" << p << ") ";
    }

    cout << endl;
}

int main()
{
    set<string> s1;

    auto ret = s1.emplace("ten");

    if (!ret.second){
        cout << "Emplace failed, element with value \"ten\" already exists."
            << endl << "  The existing element is (" << *ret.first << ")"
            << endl;
        cout << "set not modified" << endl;
    }
    else{
        cout << "set modified, now contains ";
        print(s1);
    }
    cout << endl;

    ret = s1.emplace("ten");

    if (!ret.second){
        cout << "Emplace failed, element with value \"ten\" already exists."
            << endl << "  The existing element is (" << *ret.first << ")"
            << endl;
    }
    else{
        cout << "set modified, now contains ";
        print(s1);
    }
    cout << endl;
}

emplace_hint

インプレースで構築された (コピーまたは移動操作が実行されない) 要素を、配置ヒントと一緒に挿入します。

template <class... Args>
iterator emplace_hint(
    const_iterator where,
    Args&&... args);

パラメーター

args
挿入される要素をセットがまだ含んでいない場合、より一般的には、値が同じ順序付けになる要素がセットにまだ含まれていない場合に、セットに挿入される要素を構築するために転送される引数。

where
正しい挿入ポイントの検索を開始する場所 (その位置が where の直前にある場合、挿入処理は対数時間ではなく償却定数時間で実行できます)。

戻り値

新しく挿入される要素を指す反復子。

要素が既に存在するために挿入が失敗した場合は、既存の要素を指す反復子を返します。

解説

この関数では、反復子や参照は無効になりません。

配置の実行中、例外がスローされるとコンテナーの状態は変更されません。

// set_emplace.cpp
// compile with: /EHsc
#include <set>
#include <string>
#include <iostream>

using namespace std;

template <typename S> void print(const S& s) {
    cout << s.size() << " elements: " << endl;

    for (const auto& p : s) {
        cout << "(" << p <<  ") ";
    }

    cout << endl;
}

int main()
{
    set<string> s1;

    // Emplace some test data
    s1.emplace("Anna");
    s1.emplace("Bob");
    s1.emplace("Carmine");

    cout << "set starting data: ";
    print(s1);
    cout << endl;

    // Emplace with hint
    // s1.end() should be the "next" element after this emplacement
    s1.emplace_hint(s1.end(), "Doug");

    cout << "set modified, now contains ";
    print(s1);
    cout << endl;
}

empty

set が空かどうかをテストします。

bool empty() const;

戻り値

set が空の場合は true、set が空ではない場合は false

// set_empty.cpp
// compile with: /EHsc
#include <set>
#include <iostream>

int main( )
{
   using namespace std;
   set <int> s1, s2;
   s1.insert ( 1 );

   if ( s1.empty( ) )
      cout << "The set s1 is empty." << endl;
   else
      cout << "The set s1 is not empty." << endl;

   if ( s2.empty( ) )
      cout << "The set s2 is empty." << endl;
   else
      cout << "The set s2 is not empty." << endl;
}
The set s1 is not empty.
The set s2 is empty.

end

末尾超え反復子を返します。

const_iterator end() const;

iterator end();

戻り値

末尾超え反復子。 set が空の場合は、set::end() == set::begin()

解説

end は、反復子が set の末尾を超えたかどうかをテストするために使用します。

end によって返された値は逆参照しないでください。

コード例については、「set::find」を参照してください。

equal_range

指定したキー以上のキーを持つ、set 内の最初の要素を指す反復子のペア、およびそのキーより大きいキーを持つ、set 内の最初の要素を指す反復子のペアを返します。

pair <const_iterator, const_iterator> equal_range (const Key& key) const;

pair <iterator, iterator> equal_range (const Key& key);

パラメーター

key
検索対象の set 内の要素の並べ替えキーと比較される引数キー。

戻り値

1 番目がそのキーの lower_bound、2 番目がそのキーの upper_bound である、反復子のペア。

このメンバー関数によって返されるペア pr の 1 番目の反復子にアクセスするには、pr. 最初に、下限反復子を逆参照するには、*( を使用します pr1 つ目)。 このメンバー関数によって返されるペア pr の 2 番目の反復子にアクセスするには、pr. に、上限の反復子を逆参照するには、*( を使用します pr2 番目)。

// set_equal_range.cpp
// compile with: /EHsc
#include <set>
#include <iostream>

int main( )
{
   using namespace std;
   typedef set<int, less< int > > IntSet;
   IntSet s1;
   set <int, less< int > > :: const_iterator s1_RcIter;

   s1.insert( 10 );
   s1.insert( 20 );
   s1.insert( 30 );

   pair <IntSet::const_iterator, IntSet::const_iterator> p1, p2;
   p1 = s1.equal_range( 20 );

   cout << "The upper bound of the element with "
        << "a key of 20 in the set s1 is: "
        << *(p1.second) << "." << endl;

   cout << "The lower bound of the element with "
        << "a key of 20 in the set s1 is: "
        << *(p1.first) << "." << endl;

   // Compare the upper_bound called directly
   s1_RcIter = s1.upper_bound( 20 );
   cout << "A direct call of upper_bound( 20 ) gives "
        << *s1_RcIter << "," << endl
        << "matching the 2nd element of the pair"
        << " returned by equal_range( 20 )." << endl;

   p2 = s1.equal_range( 40 );

   // If no match is found for the key,
   // both elements of the pair return end( )
   if ( ( p2.first == s1.end( ) ) && ( p2.second == s1.end( ) ) )
      cout << "The set s1 doesn't have an element "
           << "with a key less than 40." << endl;
   else
      cout << "The element of set s1 with a key >= 40 is: "
           << *(p1.first) << "." << endl;
}
The upper bound of the element with a key of 20 in the set s1 is: 30.
The lower bound of the element with a key of 20 in the set s1 is: 20.
A direct call of upper_bound( 20 ) gives 30,
matching the 2nd element of the pair returned by equal_range( 20 ).
The set s1 doesn't have an element with a key less than 40.

erase

セット内の要素または要素の範囲を指定した位置から削除するか、または指定したキーと一致する要素を削除します。

iterator erase(
    const_iterator Where);

iterator erase(
    const_iterator First,
    const_iterator Last);

size_type erase(
    const key_type& Key);

パラメーター

Where
削除される要素の位置。

First
削除される最初の要素の位置。

Last
削除される最後の要素の次の位置。

Key
削除される要素のキー値。

戻り値

最初の 2 つのメンバー関数の場合は、削除された要素の後の最初の残存要素、またはセットの最後の要素 (このような要素が存在しない場合) を指定する双方向反復子。

3 番目のメンバー関数の場合は、セットから削除された要素の数を返します。

// set_erase.cpp
// compile with: /EHsc
#include <set>
#include <string>
#include <iostream>
#include <iterator> // next() and prev() helper functions

using namespace std;

using myset = set<string>;

void printset(const myset& s) {
    for (const auto& iter : s) {
        cout << " [" << iter << "]";
    }
    cout << endl << "size() == " << s.size() << endl << endl;
}

int main()
{
    myset s1;

    // Fill in some data to test with, one at a time
    s1.insert("Bob");
    s1.insert("Robert");
    s1.insert("Bert");
    s1.insert("Rob");
    s1.insert("Bobby");

    cout << "Starting data of set s1 is:" << endl;
    printset(s1);
    // The 1st member function removes an element at a given position
    s1.erase(next(s1.begin()));
    cout << "After the 2nd element is deleted, the set s1 is:" << endl;
    printset(s1);

    // Fill in some data to test with, one at a time, using an initializer list
    myset s2{ "meow", "hiss", "purr", "growl", "yowl" };

    cout << "Starting data of set s2 is:" << endl;
    printset(s2);
    // The 2nd member function removes elements
    // in the range [First, Last)
    s2.erase(next(s2.begin()), prev(s2.end()));
    cout << "After the middle elements are deleted, the set s2 is:" << endl;
    printset(s2);

    myset s3;

    // Fill in some data to test with, one at a time, using emplace
    s3.emplace("C");
    s3.emplace("C#");
    s3.emplace("D");
    s3.emplace("D#");
    s3.emplace("E");
    s3.emplace("E#");
    s3.emplace("F");
    s3.emplace("F#");
    s3.emplace("G");
    s3.emplace("G#");
    s3.emplace("A");
    s3.emplace("A#");
    s3.emplace("B");

    cout << "Starting data of set s3 is:" << endl;
    printset(s3);
    // The 3rd member function removes elements with a given Key
    myset::size_type count = s3.erase("E#");
    // The 3rd member function also returns the number of elements removed
    cout << "The number of elements removed from s3 is: " << count << "." << endl;
    cout << "After the element with a key of \"E#\" is deleted, the set s3 is:" << endl;
    printset(s3);
}

find

指定したキーと等価のキーを持つ、set 内の要素の位置を参照する反復子を返します。

iterator find(const Key& key);

const_iterator find(const Key& key) const;

パラメーター

key
検索対象の set 内の要素の並べ替えキーと照合するキー値。

戻り値

指定したキーを持つ要素の位置を参照する反復子。キーの一致が検出されない場合は、set 内の最後の要素の次の位置 (set::end())。

解説

このメンバー関数は、小なり比較関係に基づいて順序を推論する二項述語に即して、キーが引数 key と等価である set 内の要素を参照する反復子を返します。

find の戻り値が const_iterator に割り当てられている場合、set オブジェクトは変更できません。 find の戻り値が iterator に割り当てられる場合は、set オブジェクトを変更できます

// compile with: /EHsc /W4 /MTd
#include <set>
#include <iostream>
#include <vector>
#include <string>

using namespace std;

template <typename T> void print_elem(const T& t) {
    cout << "(" << t << ") ";
}

template <typename T> void print_collection(const T& t) {
    cout << t.size() << " elements: ";

    for (const auto& p : t) {
        print_elem(p);
    }
    cout << endl;
}

template <typename C, class T> void findit(const C& c, T val) {
    cout << "Trying find() on value " << val << endl;
    auto result = c.find(val);
    if (result != c.end()) {
        cout << "Element found: "; print_elem(*result); cout << endl;
    } else {
        cout << "Element not found." << endl;
    }
}

int main()
{
    set<int> s1({ 40, 45 });
    cout << "The starting set s1 is: " << endl;
    print_collection(s1);

    vector<int> v;
    v.push_back(43);
    v.push_back(41);
    v.push_back(46);
    v.push_back(42);
    v.push_back(44);
    v.push_back(44); // attempt a duplicate

    cout << "Inserting the following vector data into s1: " << endl;
    print_collection(v);

    s1.insert(v.begin(), v.end());

    cout << "The modified set s1 is: " << endl;
    print_collection(s1);
    cout << endl;
    findit(s1, 45);
    findit(s1, 6);
}

get_allocator

set の構築に使用されるアロケーター オブジェクトのコピーを返します。

allocator_type get_allocator() const;

戻り値

set がメモリの管理に使用するアロケーターである、テンプレート パラメーター Allocator

Allocator の詳細については、set クラスのトピックのコメントのセクションをご覧ください。

解説

set クラスのアロケーターは、クラスがどのようにストレージを管理するかを指定します。 C++ 標準ライブラリ コンテナー クラスで提供される既定のアロケーターは、ほとんどのプログラミング要件に対応しています。 独自のアロケーター クラスを作成して使用することは、C++ における高度な作業の 1 つです。

// set_get_allocator.cpp
// compile with: /EHsc
#include <set>
#include <iostream>

int main( )
{
   using namespace std;
   set <int>::allocator_type s1_Alloc;
   set <int>::allocator_type s2_Alloc;
   set <double>::allocator_type s3_Alloc;
   set <int>::allocator_type s4_Alloc;

   // The following lines declare objects
   // that use the default allocator.
   set <int> s1;
   set <int, allocator<int> > s2;
   set <double, allocator<double> > s3;

   s1_Alloc = s1.get_allocator( );
   s2_Alloc = s2.get_allocator( );
   s3_Alloc = s3.get_allocator( );

   cout << "The number of integers that can be allocated"
        << endl << "before free memory is exhausted: "
        << s2.max_size( ) << "." << endl;

   cout << "\nThe number of doubles that can be allocated"
        << endl << "before free memory is exhausted: "
        << s3.max_size( ) <<  "." << endl;

   // The following line creates a set s4
   // with the allocator of multiset s1.
   set <int> s4( less<int>( ), s1_Alloc );

   s4_Alloc = s4.get_allocator( );

   // Two allocators are interchangeable if
   // storage allocated from each can be
   // deallocated by the other
   if( s1_Alloc == s4_Alloc )
   {
      cout << "\nThe allocators are interchangeable."
           << endl;
   }
   else
   {
      cout << "\nThe allocators are not interchangeable."
           << endl;
   }
}

insert

set に要素または要素範囲を挿入します。

// (1) single element
pair<iterator, bool> insert(
    const value_type& Val);

// (2) single element, perfect forwarded
template <class ValTy>
pair<iterator, bool>
insert(
    ValTy&& Val);

// (3) single element with hint
iterator insert(
    const_iterator Where,
    const value_type& Val);

// (4) single element, perfect forwarded, with hint
template <class ValTy>
iterator insert(
    const_iterator Where,
    ValTy&& Val);

// (5) range
template <class InputIterator>
void insert(
    InputIterator First,
    InputIterator Last);

// (6) initializer list
void insert(
    initializer_list<value_type>
IList);

パラメーター

Val
値が同じ順序付けになる要素が set にまだ含まれていない場合に、set に挿入される要素の値。

Where
正しい挿入ポイントの検索を開始する場所 (その位置が Where の直前にある場合、挿入処理は対数時間ではなく償却定数時間で実行できます)。

ValTy
set が value_type の要素を構築するために使用できる引数の型を指定し、Val を引数として完全転送するテンプレート パラメーター。

First
コピーされる最初の要素の位置。

Last
コピーされる最後の要素の次の位置。

InputIterator
入力反復子の要件を満たすテンプレート関数の引数。これは、value_type オブジェクトの構築に使用できる型の要素を指し示します。

IList
要素のコピー元の initializer_list

戻り値

単一要素のメンバー関数 (1) および (2) は、pairを返します。このペアの bool コンポーネントは、挿入が行われた場合は true になり、順序の値が同じ要素が set に既に含まれている場合は false になります。 戻り値であるペアの反復子コンポーネントは、bool コンポーネントが true の場合は新しく挿入される要素を指し、bool コンポーネントが false の場合は既存の要素を指します。

単一要素の with-hint メンバー関数 (3) および (4) は、set に挿入された新しい要素の位置を指す反復子を返します。ただし、同じキーを持つ要素が既に存在する場合、この反復子は既存の要素を指します。

解説

この関数では、反復子、ポインター、参照は無効になりません。

要素を 1 つだけ挿入するとき、例外がスローされるとコンテナーの状態は変更されません。 複数の要素を挿入するときに例外がスローされた場合、コンテナーの状態は未指定ですが、有効な状態になっています。

単一要素のメンバー関数によって返される pairpr の反復子コンポーネントにアクセスするには、pr.first を使用します。返されるペアに含まれる反復子を逆参照するには、要素を指定して、*pr.first を使用します。 bool コンポーネントにアクセスするには、pr.second を使用します。 例については、この記事で後ほど説明するサンプル コードを参照してください。

コンテナーの value_type は、コンテナーに属する typedef であり、set の場合、set<V>::value_typeconst V 型です。

範囲を指定したメンバー関数 (5) は、範囲 [First, Last) の反復子によってアドレス指定された各要素に対応する set に要素値のシーケンスを挿入します。したがって、Last は挿入されません。 コンテナーのメンバー関数 end() は、コンテナー内にある最後の要素の直後の位置を参照します。たとえば、ステートメント s.insert(v.begin(), v.end()); は、v のすべての要素を s に挿入しようとします。 範囲内で一意の値を持つ要素だけが挿入されますが、値が重複する要素は無視されます。 拒否される要素を確認するには、1 つの要素が指定された insert を使用します。

初期化子リストのメンバー関数 (6) は、initializer_list を使用して set に要素をコピーします。

インプレースで構築された (つまり、コピーまたは移動操作が実行されない) 要素の挿入については、「set::emplace」および「set::emplace_hint」を参照してください。

// set_insert.cpp
// compile with: /EHsc
#include <set>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

template <typename S> void print(const S& s) {
    cout << s.size() << " elements: ";

    for (const auto& p : s) {
        cout << "(" << p << ") ";
    }

    cout << endl;
}

int main()
{
    // insert single values
    set<int> s1;
    // call insert(const value_type&) version
    s1.insert({ 1, 10 });
    // call insert(ValTy&&) version
    s1.insert(20);

    cout << "The original set values of s1 are:" << endl;
    print(s1);

    // intentionally attempt a duplicate, single element
    auto ret = s1.insert(1);
    if (!ret.second){
        auto elem = *ret.first;
        cout << "Insert failed, element with value 1 already exists."
            << endl << "  The existing element is (" << elem << ")"
            << endl;
    }
    else{
        cout << "The modified set values of s1 are:" << endl;
        print(s1);
    }
    cout << endl;

    // single element, with hint
    s1.insert(s1.end(), 30);
    cout << "The modified set values of s1 are:" << endl;
    print(s1);
    cout << endl;

    // The templatized version inserting a jumbled range
    set<int> s2;
    vector<int> v;
    v.push_back(43);
    v.push_back(294);
    v.push_back(41);
    v.push_back(330);
    v.push_back(42);
    v.push_back(45);

    cout << "Inserting the following vector data into s2:" << endl;
    print(v);

    s2.insert(v.begin(), v.end());

    cout << "The modified set values of s2 are:" << endl;
    print(s2);
    cout << endl;

    // The templatized versions move-constructing elements
    set<string>  s3;
    string str1("blue"), str2("green");

    // single element
    s3.insert(move(str1));
    cout << "After the first move insertion, s3 contains:" << endl;
    print(s3);

    // single element with hint
    s3.insert(s3.end(), move(str2));
    cout << "After the second move insertion, s3 contains:" << endl;
    print(s3);
    cout << endl;

    set<int> s4;
    // Insert the elements from an initializer_list
    s4.insert({ 4, 44, 2, 22, 3, 33, 1, 11, 5, 55 });
    cout << "After initializer_list insertion, s4 contains:" << endl;
    print(s4);
    cout << endl;
}

iterator

セット内の任意の要素を読み取れる、定数の双方向反復子を提供する型。

typedef implementation-defined iterator;

iterator の宣言や使用の方法の例については、begin の例を参照してください。

key_comp

set 内のキーを並べ替えるために使用される比較オブジェクトのコピーを取得します。

key_compare key_comp() const;

戻り値

set が要素の並べ替えに使用する関数オブジェクトである、テンプレート パラメーター Traits を返します。

Traits の詳細については、set クラスのトピックをご覧ください。

解説

格納されているオブジェクトはメンバー関数を定義します。

bool operator()(const Key>_xVal, const Key>_yVal;

これは、並べ替え順で _xVal_yVal に先行しかつ等しくない場合に true を返します。

key_comparevalue_compare はどちらもテンプレート パラメーター Traits のシノニムです。 これらの型は map および multimap クラスでは異なるものになるため、互換性を保つためにこれらが同一のものである set および multiset クラスでも使用できるようになっています。

// set_key_comp.cpp
// compile with: /EHsc
#include <set>
#include <iostream>

int main( )
{
   using namespace std;

   set <int, less<int> > s1;
   set<int, less<int> >::key_compare kc1 = s1.key_comp( ) ;
   bool result1 = kc1( 2, 3 ) ;
   if( result1 == true )
   {
      cout << "kc1( 2,3 ) returns value of true, "
           << "where kc1 is the function object of s1."
           << endl;
   }
   else
   {
      cout << "kc1( 2,3 ) returns value of false "
           << "where kc1 is the function object of s1."
           << endl;
   }

   set <int, greater<int> > s2;
   set<int, greater<int> >::key_compare kc2 = s2.key_comp( ) ;
   bool result2 = kc2( 2, 3 ) ;
   if(result2 == true)
   {
      cout << "kc2( 2,3 ) returns value of true, "
           << "where kc2 is the function object of s2."
           << endl;
   }
   else
   {
      cout << "kc2( 2,3 ) returns value of false, "
           << "where kc2 is the function object of s2."
           << endl;
   }
}
kc1( 2,3 ) returns value of true, where kc1 is the function object of s1.
kc2( 2,3 ) returns value of false, where kc2 is the function object of s2.

key_compare

2 つの並べ替えキーを比較して、set 内の 2 つの要素の相対順序を決定できる関数オブジェクトを提供する型。

typedef Traits key_compare;

解説

key_compare は、テンプレート パラメーター Traits のシノニムです。

Traits の詳細については、set クラスのトピックをご覧ください。

key_comparevalue_compare はどちらもテンプレート パラメーター Traits のシノニムです。 これらの型は map および multimap クラスでは異なるものになるため、互換性を保つためにこれらが同一のものである set および multiset クラスでも使用できるようになっています。

key_compare の宣言や使用の方法の例については、key_comp の例を参照してください。

key_type

並べ替えキーとしてキャパシティ内に set の要素として格納されるオブジェクトを表す型。

typedef Key key_type;

解説

key_type は、テンプレート パラメーター Key のシノニムです。

Key の詳細については、set クラスのトピックのコメントのセクションをご覧ください。

key_typevalue_type はどちらもテンプレート パラメーター Key のシノニムです。 これらの型は map および multimap クラスでは異なるものになるため、互換性を保つためにこれらが同一のものである set および multiset クラスでも使用できるようになっています。

key_type の宣言や使用の方法の例については、value_type の例を参照してください。

lower_bound

指定したキー以上のキーを持つ、set 内の最初の要素を指す反復子を返します。

const_iterator lower_bound(const Key& key) const;

iterator lower_bound(const Key& key);

パラメーター

key
検索対象の set 内の要素の並べ替えキーと比較される引数キー。

戻り値

引数キー以上のキーを持つ set 内の要素の位置を指す、または、キーの一致が検出されない場合は set 内の最後の要素の次の位置を指す反復子または const_iterator

// set_lower_bound.cpp
// compile with: /EHsc
#include <set>
#include <iostream>

int main( )
{
   using namespace std;
   set <int> s1;
   set <int> :: const_iterator s1_AcIter, s1_RcIter;

   s1.insert( 10 );
   s1.insert( 20 );
   s1.insert( 30 );

   s1_RcIter = s1.lower_bound( 20 );
   cout << "The element of set s1 with a key of 20 is: "
        << *s1_RcIter << "." << endl;

   s1_RcIter = s1.lower_bound( 40 );

   // If no match is found for the key, end( ) is returned
   if ( s1_RcIter == s1.end( ) )
      cout << "The set s1 doesn't have an element "
           << "with a key of 40." << endl;
   else
      cout << "The element of set s1 with a key of 40 is: "
           << *s1_RcIter << "." << endl;

   // The element at a specific location in the set can be found
   // by using a dereferenced iterator that addresses the location
   s1_AcIter = s1.end( );
   s1_AcIter--;
   s1_RcIter = s1.lower_bound( *s1_AcIter );
   cout << "The element of s1 with a key matching "
        << "that of the last element is: "
        << *s1_RcIter << "." << endl;
}
The element of set s1 with a key of 20 is: 20.
The set s1 doesn't have an element with a key of 40.
The element of s1 with a key matching that of the last element is: 30.

max_size

set の最大長を返します。

size_type max_size() const;

戻り値

set の可能な最大長。

// set_max_size.cpp
// compile with: /EHsc
#include <set>
#include <iostream>

int main( )
{
   using namespace std;
   set <int> s1;
   set <int>::size_type i;

   i = s1.max_size( );
   cout << "The maximum possible length "
        << "of the set is " << i << "." << endl;
}

operator=

この set の要素を、別の set の要素を使って置き換えます。

set& operator=(const set& right);

set& operator=(set&& right);

パラメーター

right
この set に割り当てられる新しい要素を提供する set

解説

1 番目のバージョンの operator= は、right への 左辺値参照を使用して right からこの set に要素をコピーします。

2 番目のバージョンは、right への rvalue 参照を使用します。 このバージョンでは、right の要素が set に移されます。

これらの演算子関数の実行前にこの set 内に含まれていた要素はすべて破棄されます。

// set_operator_as.cpp
// compile with: /EHsc
#include <set>
#include <iostream>

int main( )
   {
   using namespace std;
   set<int> v1, v2, v3;
   set<int>::iterator iter;

   v1.insert(10);

   cout << "v1 = " ;
   for (iter = v1.begin(); iter != v1.end(); iter++)
      cout << *iter << " ";
   cout << endl;

   v2 = v1;
   cout << "v2 = ";
   for (iter = v2.begin(); iter != v2.end(); iter++)
      cout << *iter << " ";
   cout << endl;

// move v1 into v2
   v2.clear();
   v2 = move(v1);
   cout << "v2 = ";
   for (iter = v2.begin(); iter != v2.end(); iter++)
      cout << *iter << " ";
   cout << endl;
   }

pointer

set 内の要素へのポインターを提供する型。

typedef typename allocator_type::pointer pointer;

解説

pointer は要素の値の変更に使用できます。

ほとんどの場合、set オブジェクト内の要素にアクセスするには、iterator を使用する必要があります。

rbegin

反転された set 内の最初の要素を指す反復子を返します。

const_reverse_iterator rbegin() const;

reverse_iterator rbegin();

戻り値

反転された set 内の最初の要素を示す、または反転されていない set 内の最後の要素だったものを示す反転双方向反復子。

解説

rbegin は、begin が set で使用されるように、逆順の set で使用されます。

rbegin の戻り値が const_reverse_iterator に割り当てられる場合は、set オブジェクトを変更できません。 rbegin の戻り値が reverse_iterator に割り当てられる場合は、set オブジェクトを変更できます。

rbegin を使用して、set 内を後方に向かって反復処理できます。

// set_rbegin.cpp
// compile with: /EHsc
#include <set>
#include <iostream>

int main( )
{
   using namespace std;
   set <int> s1;
   set <int>::iterator s1_Iter;
   set <int>::reverse_iterator s1_rIter;

   s1.insert( 10 );
   s1.insert( 20 );
   s1.insert( 30 );

   s1_rIter = s1.rbegin( );
   cout << "The first element in the reversed set is "
        << *s1_rIter << "." << endl;

   // begin can be used to start an iteration
   // through a set in a forward order
   cout << "The set is:";
   for ( s1_Iter = s1.begin( ) ; s1_Iter != s1.end( ); s1_Iter++ )
      cout << " " << *s1_Iter;
   cout << endl;

   // rbegin can be used to start an iteration
   // through a set in a reverse order
   cout << "The reversed set is:";
   for ( s1_rIter = s1.rbegin( ) ; s1_rIter != s1.rend( ); s1_rIter++ )
      cout << " " << *s1_rIter;
   cout << endl;

   // A set element can be erased by dereferencing to its key
   s1_rIter = s1.rbegin( );
   s1.erase ( *s1_rIter );

   s1_rIter = s1.rbegin( );
   cout << "After the erasure, the first element "
        << "in the reversed set is "<< *s1_rIter << "." << endl;
}
The first element in the reversed set is 30.
The set is: 10 20 30
The reversed set is: 30 20 10
After the erasure, the first element in the reversed set is 20.

reference

set に格納されている要素への参照を提供する型。

typedef typename allocator_type::reference reference;

// set_reference.cpp
// compile with: /EHsc
#include <set>
#include <iostream>

int main( )
{
   using namespace std;
   set <int> s1;

   s1.insert( 10 );
   s1.insert( 20 );

   // Declare and initialize a reference &Ref1 to the 1st element
   const int &Ref1 = *s1.begin( );

   cout << "The first element in the set is "
        << Ref1 << "." << endl;
}
The first element in the set is 10.

rend

反転された set 内の最後の要素の次の位置を指す反復子を返します。

const_reverse_iterator rend() const;

reverse_iterator rend();

戻り値

逆順の set 内の最後の要素の次の場所 (通常の順序の set 内の最初の要素の前の場所) を指す逆順双方向反復子。

解説

rend は、end が set で使用されるように、逆順の set で使用されます。

rend の戻り値が const_reverse_iterator に割り当てられる場合は、set オブジェクトを変更できません。 rend の戻り値が reverse_iterator に割り当てられる場合は、set オブジェクトを変更できます。 rend によって返された値は逆参照しないでください。

rend を使用して、逆順反復子が set の末尾に達したかどうかをテストできます。

// set_rend.cpp
// compile with: /EHsc
#include <set>
#include <iostream>

int main() {
   using namespace std;
   set <int> s1;
   set <int>::iterator s1_Iter;
   set <int>::reverse_iterator s1_rIter;
   set <int>::const_reverse_iterator s1_crIter;

   s1.insert( 10 );
   s1.insert( 20 );
   s1.insert( 30 );

   s1_rIter = s1.rend( );
   s1_rIter--;
   cout << "The last element in the reversed set is "
        << *s1_rIter << "." << endl;

   // end can be used to terminate an iteration
   // through a set in a forward order
   cout << "The set is: ";
   for ( s1_Iter = s1.begin( ) ; s1_Iter != s1.end( ); s1_Iter++ )
      cout << *s1_Iter << " ";
   cout << "." << endl;

   // rend can be used to terminate an iteration
   // through a set in a reverse order
   cout << "The reversed set is: ";
   for ( s1_rIter = s1.rbegin( ) ; s1_rIter != s1.rend( ); s1_rIter++ )
      cout << *s1_rIter << " ";
   cout << "." << endl;

   s1_rIter = s1.rend( );
   s1_rIter--;
   s1.erase ( *s1_rIter );

   s1_rIter = s1.rend( );
   --s1_rIter;
   cout << "After the erasure, the last element in the "
        << "reversed set is " << *s1_rIter << "." << endl;
}

reverse_iterator

反転された set 内の 1 つの要素の読み取りまたは変更ができる双方向反復子を提供する型。

typedef std::reverse_iterator<iterator> reverse_iterator;

解説

reverse_iterator は、逆の順序で set を反復処理するために使用します。

reverse_iterator の宣言や使用の方法の例については、rbegin の例を参照してください。

set

空の set を構築するか、他の set の全体または一部のコピーである set を構築します。

set();

explicit set(
    const Traits& Comp);

set(
    const Traits& Comp,
    const Allocator& Al);

set(
    const set& Right);

set(
    set&& Right);

set(
    initializer_list<Type> IList);

set(
    initializer_list<Type> IList,
    const Compare& Comp);

set(
    initializer_list<Type> IList,
    const Compare& Comp,
    const Allocator& Al);

template <class InputIterator>
set(
    InputIterator First,
    InputIterator Last);

template <class InputIterator>
set(
    InputIterator First,
    InputIterator Last,
    const Traits& Comp);

template <class InputIterator>
set(
    InputIterator First,
    InputIterator Last,
    const Traits& Comp,
    const Allocator& Al);

パラメーター

Al
この set オブジェクトに使用するストレージ アロケーター クラス。既定では、Allocator です。

Comp
set 内の要素の並べ替えに使用される、const Traits 型の比較関数。既定では Compare です。

Rght
構築される set のコピー元となる set。

First
コピーする要素範囲内の最初の要素の位置。

Last
コピーする要素範囲を超える最初の要素の位置。

IList
要素のコピー元の initializer_list。

解説

すべてのコンストラクターは、アロケーター オブジェクトの型を格納します。このオブジェクトは set のメモリ ストレージを管理し、後で get_allocator を呼び出して取得することができます。 代替アロケーターの代わりに使用されるクラス宣言やプリプロセス マクロでは、アロケーターのパラメーターが省略される場合があります。

すべてのコンストラクターは、それぞれの set を初期化します。

すべてのコンストラクターは、Traits 型の関数オブジェクトを格納します。このオブジェクトは set のキーの順序を確立するために使用され、後で key_comp を呼び出して取得できます。

最初の 3 つのコンストラクターは、空の初期 set を指定します。2 番目のコンストラクターは要素の順序を確立するために使用する比較関数の型 (comp) を指定し、3 番目のコンストラクターは使用するアロケーターの型 (al) を明示的に指定します。 キーワード explicit は、特定の種類の自動型変換が実行されないようにします。

4 番目のコンストラクターは、set right のコピーを指定します。

次の 3 つのコンストラクターは、initializer_list を使用して要素を指定します。

次の 3 つのコンストラクターは、set の範囲 [ first, last) をコピーします。下のコンストラクターになるほど、より明確に Traits クラスの比較関数と Allocator の型が指定されています。

8 番目のコンストラクターは、right を移動することによって、set のコピーを指定します。

// set_set.cpp
// compile with: /EHsc
#include <set>
#include <iostream>

int main()
{
    using namespace std;

    // Create an empty set s0 of key type integer
    set <int> s0;

    // Create an empty set s1 with the key comparison
    // function of less than, then insert 4 elements
    set <int, less<int> > s1;
    s1.insert(10);
    s1.insert(20);
    s1.insert(30);
    s1.insert(40);

    // Create an empty set s2 with the key comparison
    // function of less than, then insert 2 elements
    set <int, less<int> > s2;
    s2.insert(10);
    s2.insert(20);

    // Create a set s3 with the
    // allocator of set s1
    set <int>::allocator_type s1_Alloc;
    s1_Alloc = s1.get_allocator();
    set <int> s3(less<int>(), s1_Alloc);
    s3.insert(30);

    // Create a copy, set s4, of set s1
    set <int> s4(s1);

    // Create a set s5 by copying the range s1[ first,  last)
    set <int>::const_iterator s1_bcIter, s1_ecIter;
    s1_bcIter = s1.begin();
    s1_ecIter = s1.begin();
    s1_ecIter++;
    s1_ecIter++;
    set <int> s5(s1_bcIter, s1_ecIter);

    // Create a set s6 by copying the range s4[ first,  last)
    // and with the allocator of set s2
    set <int>::allocator_type s2_Alloc;
    s2_Alloc = s2.get_allocator();
    set <int> s6(s4.begin(), ++s4.begin(), less<int>(), s2_Alloc);

    cout << "s1 =";
    for (auto i : s1)
        cout << " " << i;
    cout << endl;

    cout << "s2 = " << *s2.begin() << " " << *++s2.begin() << endl;

    cout << "s3 =";
    for (auto i : s3)
        cout << " " << i;
    cout << endl;

    cout << "s4 =";
    for (auto i : s4)
        cout << " " << i;
    cout << endl;

    cout << "s5 =";
    for (auto i : s5)
        cout << " " << i;
    cout << endl;

    cout << "s6 =";
    for (auto i : s6)
        cout << " " << i;
    cout << endl;

    // Create a set by moving s5
    set<int> s7(move(s5));
    cout << "s7 =";
    for (auto i : s7)
        cout << " " << i;
    cout << endl;

    // Create a set with an initializer_list
    cout << "s8 =";
    set<int> s8{ { 1, 2, 3, 4 } };
    for (auto i : s8)
        cout << " " << i;
    cout << endl;

    cout << "s9 =";
    set<int> s9{ { 5, 6, 7, 8 }, less<int>() };
    for (auto i : s9)
        cout << " " << i;
    cout << endl;

    cout << "s10 =";
    set<int> s10{ { 10, 20, 30, 40 }, less<int>(), s9.get_allocator() };
    for (auto i : s10)
        cout << " " << i;
    cout << endl;
}
s1 = 10 20 30 40s2 = 10 20s3 = 30s4 = 10 20 30 40s5 = 10 20s6 = 10s7 = 10 20s8 = 1 2 3 4s9 = 5 6 7 8s10 = 10 20 30 40

size

セット内の要素数を返します。

size_type size() const;

戻り値

set の現在の長さ。

// set_size.cpp
// compile with: /EHsc
#include <set>
#include <iostream>

int main( )
{
   using namespace std;
   set <int> s1;
   set <int> :: size_type i;

   s1.insert( 1 );
   i = s1.size( );
   cout << "The set length is " << i << "." << endl;

   s1.insert( 2 );
   i = s1.size( );
   cout << "The set length is now " << i << "." << endl;
}
The set length is 1.
The set length is now 2.

size_type

set 内の要素の数を表すことができる符号なし整数型。

typedef typename allocator_type::size_type size_type;

size_type の宣言や使用の方法の例については、size の例を参照してください

swap

2 つの set の要素を交換します。

void swap(
    set<Key, Traits, Allocator>& right);

パラメーター

right
ターゲットの set と交換する要素を提供する引数の set。

解説

このメンバー関数が、要素を交換する 2 つの set において要素を指定している参照、ポインター、反復子を無効化することはありません。

// set_swap.cpp
// compile with: /EHsc
#include <set>
#include <iostream>

int main( )
{
   using namespace std;
   set <int> s1, s2, s3;
   set <int>::iterator s1_Iter;

   s1.insert( 10 );
   s1.insert( 20 );
   s1.insert( 30 );
   s2.insert( 100 );
   s2.insert( 200 );
   s3.insert( 300 );

   cout << "The original set s1 is:";
   for ( s1_Iter = s1.begin( ); s1_Iter != s1.end( ); s1_Iter++ )
      cout << " " << *s1_Iter;
   cout   << "." << endl;

   // This is the member function version of swap
   s1.swap( s2 );

   cout << "After swapping with s2, list s1 is:";
   for ( s1_Iter = s1.begin( ); s1_Iter != s1.end( ); s1_Iter++ )
      cout << " " << *s1_Iter;
   cout  << "." << endl;

   // This is the specialized template version of swap
   swap( s1, s3 );

   cout << "After swapping with s3, list s1 is:";
   for ( s1_Iter = s1.begin( ); s1_Iter != s1.end( ); s1_Iter++ )
      cout << " " << *s1_Iter;
   cout   << "." << endl;
}
The original set s1 is: 10 20 30.
After swapping with s2, list s1 is: 100 200.
After swapping with s3, list s1 is: 300.

upper_bound

指定したキーよりも大きいキーを持つ、set 内の最初の要素を指す反復子を返します。

const_iterator upper_bound(const Key& key) const;

iterator upper_bound(const Key& key);

パラメーター

key
検索対象の set 内の要素の並べ替えキーと比較される引数キー。

戻り値

引数キー以上のキーを持つ set 内の要素の位置を指す、または、キーの一致が検出されない場合は set 内の最後の要素の次の位置を指す iterator または const_iterator

// set_upper_bound.cpp
// compile with: /EHsc
#include <set>
#include <iostream>

int main( )
{
   using namespace std;
   set <int> s1;
   set <int> :: const_iterator s1_AcIter, s1_RcIter;

   s1.insert( 10 );
   s1.insert( 20 );
   s1.insert( 30 );

   s1_RcIter = s1.upper_bound( 20 );
   cout << "The first element of set s1 with a key greater "
        << "than 20 is: " << *s1_RcIter << "." << endl;

   s1_RcIter = s1.upper_bound( 30 );

   // If no match is found for the key, end( ) is returned
   if ( s1_RcIter == s1.end( ) )
      cout << "The set s1 doesn't have an element "
           << "with a key greater than 30." << endl;
   else
      cout << "The element of set s1 with a key > 40 is: "
           << *s1_RcIter << "." << endl;

   // The element at a specific location in the set can be found
   // by using a dereferenced iterator addressing the location
   s1_AcIter = s1.begin( );
   s1_RcIter = s1.upper_bound( *s1_AcIter );
   cout << "The first element of s1 with a key greater than"
        << endl << "that of the initial element of s1 is: "
        << *s1_RcIter << "." << endl;
}
The first element of set s1 with a key greater than 20 is: 30.
The set s1 doesn't have an element with a key greater than 30.
The first element of s1 with a key greater than
that of the initial element of s1 is: 20.

value_comp

set 内の要素の値を並べ替えるために使用される比較オブジェクトのコピーを取得します。

value_compare value_comp() const;

戻り値

set が要素の並べ替えに使用する関数オブジェクトである、テンプレート パラメーター Traits を返します。

Traits の詳細については、set クラスのトピックをご覧ください。

解説

格納されているオブジェクトはメンバー関数を定義します。

bool operator(const Key>_xVal const Key>_yVal;

これは、並べ替え順で _xVal_yVal に先行しかつ等しくない場合に true を返します。

value_comparekey_compare はどちらもテンプレート パラメーター Traits のシノニムです。 これらの型は map および multimap クラスでは異なるものになるため、互換性を保つためにこれらが同一のものである set および multiset クラスでも使用できるようになっています。

// set_value_comp.cpp
// compile with: /EHsc
#include <set>
#include <iostream>

int main( )
{
   using namespace std;

   set <int, less<int> > s1;
   set <int, less<int> >::value_compare vc1 = s1.value_comp( );
   bool result1 = vc1( 2, 3 );
   if( result1 == true )
   {
      cout << "vc1( 2,3 ) returns value of true, "
           << "where vc1 is the function object of s1."
           << endl;
   }
   else
   {
      cout << "vc1( 2,3 ) returns value of false, "
           << "where vc1 is the function object of s1."
           << endl;
   }

   set <int, greater<int> > s2;
   set<int, greater<int> >::value_compare vc2 = s2.value_comp( );
   bool result2 = vc2( 2, 3 );
   if( result2 == true )
   {
      cout << "vc2( 2,3 ) returns value of true, "
           << "where vc2 is the function object of s2."
           << endl;
   }
   else
   {
      cout << "vc2( 2,3 ) returns value of false, "
           << "where vc2 is the function object of s2."
           << endl;
   }
}
vc1( 2,3 ) returns value of true, where vc1 is the function object of s1.
vc2( 2,3 ) returns value of false, where vc2 is the function object of s2.

value_compare

2 つの要素の値を比較して、set 内の要素の相対順序を決定できる関数オブジェクトを提供する型。

typedef key_compare value_compare;

解説

value_compare は、テンプレート パラメーター Traits のシノニムです。

Traits の詳細については、set クラスのトピックをご覧ください。

key_comparevalue_compare はどちらもテンプレート パラメーター Traits のシノニムです。 これらの型は map および multimap クラスでは異なるものになるため、互換性を保つためにこれらが同一のものである set および multiset クラスでも使用できるようになっています。

value_compare の宣言や使用の方法の例については、value_comp の例を参照してください。

value_type

値として、キャパシティ内で set の要素として格納されるオブジェクトを表す型です。

typedef Key value_type;

解説

value_type は、テンプレート パラメーター Key のシノニムです。

Key の詳細については、set クラスのトピックのコメントのセクションをご覧ください。

key_typevalue_type はどちらもテンプレート パラメーター Key のシノニムです。 これらの型は map および multimap クラスでは異なるものになるため、互換性を保つためにこれらが同一のものである set および multiset クラスでも使用できるようになっています。

// set_value_type.cpp
// compile with: /EHsc
#include <set>
#include <iostream>

int main( )
{
   using namespace std;
   set <int> s1;
   set <int>::iterator s1_Iter;

   set <int>::value_type svt_Int;   // Declare value_type
   svt_Int = 10;            // Initialize value_type

   set <int> :: key_type skt_Int;   // Declare key_type
   skt_Int = 20;             // Initialize key_type

   s1.insert( svt_Int );         // Insert value into s1
   s1.insert( skt_Int );         // Insert key into s1

   // A set accepts key_types or value_types as elements
   cout << "The set has elements:";
   for ( s1_Iter = s1.begin( ) ; s1_Iter != s1.end( ); s1_Iter++)
      cout << " " << *s1_Iter;
   cout << "." << endl;
}
The set has elements: 10 20.