次の方法で共有


tuple_element クラス

tuple 要素をラップします。 array 要素および pair 要素のラップの特殊化。

構文

// CLASS tuple_element (find element by index)
template <size_t Index, class Tuple>
   struct tuple_element;

// tuple_element for const
template <size_t Index, class Tuple>
   struct tuple_element<Index, const Tuple>;

// tuple_element for volatile
template <size_t Index, class Tuple>
   struct tuple_element<Index, volatile Tuple>;

// tuple_element for const volatile
template <size_t Index, class Tuple>
   struct tuple_element<Index, const volatile Tuple>;

// Helper typedef
template <size_t Index, class Tuple>
   using tuple_element_t = typename tuple_element<Index, Tuple>::type;

// Specialization for arrays
template <size_t Index, class Elem, size_t Size>
   struct tuple_element<Index, array<Elem, Size>>;

// Specializations for pairs
// struct to determine type of element 0 in pair
template <class T1, class T2>
   struct tuple_element<0, pair<T1, T2>>;

// struct to determine type of element 1 in pair
template <class T1, class T2>
   struct tuple_element<1, pair<T1, T2>>;

パラメーター

インデックス
指定した要素のインデックス。

Tuple
組の型。

Elem
配列要素の型。

[サイズ]
配列のサイズ。

T1
ペアの 1 番目の要素の型。

T2
ペアの 2 番目の要素の型。

解説

このクラス テンプレート tuple_element では、タプル型 Tuple のインデックス Index にある型の同意語である typedef type が入れ子になっています。

Typedef tuple_element_ttuple_element<Index, Tuple>::type の便利なエイリアスです。

配列のクラス テンプレートの特殊化では、同じ型の Size 要素のタプルとして array にインターフェイスを提供します。 それぞれの特殊化は入れ子の typedef type を持ち、これは arrayIndex 要素の型の同意語であり、const-volatile 制限が保持されます。

pair の型のテンプレートの特殊化では、それぞれ 1 つのメンバーの typedef である type を提供します。これはペアの指定された位置にある要素の型のシノニムであり、const または volatile の制限が保持されます。 Typedef tuple_element_ttuple_element<N, pair<T1, T2>>::type の便利なエイリアスです。

get 関数を使用して、指定した位置の要素、または指定した型の要素を返します。

例: 組から要素を取得する

#include <tuple>
#include <string>
#include <iostream>

using namespace std;
typedef tuple<int, double, string> MyTuple;

int main() {
    MyTuple c0{ 0, 1.5, "Tail" };

    tuple_element_t<0, MyTuple> val = get<0>(c0); //get by index
    tuple_element_t<1, MyTuple> val2 = get<1>(c0);
    tuple_element_t<2, MyTuple> val3 = get<string>(c0); // get by type

    cout << val << " " << val2 << " " << val3 << endl;
}
0 1.5 Tail

例: 配列から要素を取得する

#include <array>
#include <iostream>

using namespace std;
typedef array<int, 4> MyArray;

int main()
{
    MyArray c0 { 0, 1, 2, 3 };

    for (const auto& e : c0)
    {
        cout << e << " ";
    }
    cout << endl;

    // display first element "0"
    tuple_element<0, MyArray>::type val = c0.front();
    cout << val << endl;
}
0 1 2 3
0

例: ペアから要素を取得する

#include <utility>
#include <iostream>

using namespace std;

typedef pair<int, double> MyPair;
int main() {
    MyPair c0(0, 1.333);

    // display contents "0 1"
    cout << get<0>(c0) << " ";
    cout << get<1>(c0) << endl;

    // display first element "0 " by index
    tuple_element<0, MyPair>::type val = get<0>(c0);
    cout << val << " ";

    // display second element by type
    tuple_element<1, MyPair>::type val2 = get<double>(c0);
    cout << val2 << endl;
}
0 1.333
0 1.333

必要条件

ヘッダー:<tuple>

ヘッダー:<array> (配列の特殊化用)

ヘッダー:<utility> (ペアの特殊化用)

名前空間: std