共用方式為


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>>;

參數

Index
指定之項目的索引。

Tuple
Tuple 的類型。

Elem
陣列元素的類型。

大小
陣列的大小。

T1
配對中第一個專案的類型。

T2
配對中第二個元素的類型。

備註

類別範本 tuple_element 具有巢狀 typedef type ,這與 Tuple 類型 Tuple 索引 索引處的類型 同義。

typedef tuple_element_ttuple_element<Index, Tuple>::type 的方便別名。

陣列的類別範本特製化會提供 介面 array 做為元素的 Size Tuple,每個元素都有相同的類型。 每個特製化都有一個巢狀 typedef type ,與 的 Index 元素 array 類型 同義,並保留任何 const-volatile 限定性。

pair 類型的樣板特製化都提供單一成員 typedef,type,也就是在配對中指定位置的元素類型同義字,保留了任何 const 及/或 volatile 限定性條件。 typedef tuple_element_ttuple_element<N, pair<T1, T2>>::type 的方便別名。

使用 函 get 傳回位於指定位置或指定型別的專案。

範例:從 Tuple 取得專案

#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>

Header: < array > (適用于陣列特製化)

Header: < utility > (適用于配對特製化)

命名空間:std