Bagikan melalui


Kelas tuple_element

Membungkus tuple elemen. Spesialisasi membungkus array elemen dan pair elemen.

Sintaks

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

Parameter

Index
Indeks elemen yang ditunjuk.

Tuple
Jenis tuple.

Elem
Jenis elemen array.

Ukuran
Ukuran array.

T1
Jenis elemen pertama dalam sepasang.

T2
Jenis elemen kedua dalam sepasang.

Keterangan

Templat tuple_element kelas memiliki typedef type berlapis yang merupakan sinonim untuk jenis di indeks Indeks jenis tuple Tuple.

Typedef tuple_element_t adalah alias yang nyaman untuk tuple_element<Index, Tuple>::type.

Spesialisasi templat kelas untuk array menyediakan antarmuka untuk array sebagai tuple Size elemen, yang masing-masing memiliki jenis yang sama. Setiap spesialisasi memiliki typedef type berlapis yang merupakan sinonim untuk jenis elemen Indeks dari array, dengan kualifikasi const-volatile yang dipertahankan.

Spesialisasi templat untuk pair jenis masing-masing menyediakan typedef anggota tunggal, type, yang merupakan sinonim untuk jenis elemen pada posisi yang ditentukan dalam pasangan, dengan kualifikasi kontra dan/atau volatil apa pun yang dipertahankan. Typedef tuple_element_t adalah alias yang nyaman untuk tuple_element<N, pair<T1, T2>>::type.

get Gunakan fungsi untuk mengembalikan elemen pada posisi tertentu, atau dari jenis yang ditentukan.

Contoh: Mendapatkan elemen dari 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

Contoh: Mendapatkan elemen dari array

#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

Contoh: Mendapatkan elemen dari pasangan

#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

Persyaratan

Header:<tuple>

Header:<array> (untuk spesialisasi array)

Header:<utilitas> (untuk spesialisasi pasangan)

Namespace: std