Partager via


fonctions<array>

L’en-tête <de tableau comprend deux fonctions non membres et getswap, qui fonctionnent sur des objets de tableau>.

get
swap

get

Retourne une référence à l’élément spécifié du tableau.

template <int Index, class T, size_t N>
constexpr T& get(array<T, N>& arr) noexcept;

template <int Index, class T, size_t N>
constexpr const T& get(const array<T, N>& arr) noexcept;

template <int Index, class T, size_t N>
constexpr T&& get(array<T, N>&& arr) noexcept;

Paramètres

Index
Décalage de l’élément.

T
Type d’un élément.

N
Nombre d’éléments dans le tableau.

Arr
Tableau dans lequel opérer la sélection.

Exemple

#include <array>
#include <iostream>

using namespace std;

typedef array<int, 4> MyArray;

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

    // display contents " 0 1 2 3"
    for (const auto& e : c0)
    {
        cout << " " << e;
    }
    cout << endl;

    // display odd elements " 1 3"
    cout << " " << get<1>(c0);
    cout << " " << get<3>(c0) << endl;
}
0 1 2 3
1 3

swap

Une spécialisation de modèle non membre de std::swap celui-ci échange deux objets de tableau .

template <class Ty, std::size_t N>
void swap(array<Ty, N>& left, array<Ty, N>& right);

Paramètres

Ty
Type d’un élément.

N
Taille du tableau.

left
Premier tableau à échanger.

right
Deuxième tableau à échanger.

Notes

La fonction de modèle exécute left.swap(right).

Exemple

// std__array__swap.cpp
// compile with: /EHsc
#include <array>
#include <iostream>

typedef std::array<int, 4> Myarray;
int main()
{
    Myarray c0 = { 0, 1, 2, 3 };

    // display contents " 0 1 2 3"
    for (Myarray::const_iterator it = c0.begin();
        it != c0.end(); ++it)
        std::cout << " " << *it;
    std::cout << std::endl;

    Myarray c1 = { 4, 5, 6, 7 };
    c0.swap(c1);

    // display swapped contents " 4 5 6 7"
    for (Myarray::const_iterator it = c0.begin();
        it != c0.end(); ++it)
        std::cout << " " << *it;
    std::cout << std::endl;

    swap(c0, c1);

    // display swapped contents " 0 1 2 3"
    for (Myarray::const_iterator it = c0.begin();
        it != c0.end(); ++it)
        std::cout << " " << *it;
    std::cout << std::endl;

    return (0);
}
0 1 2 3
4 5 6 7
0 1 2 3

Voir aussi

<array>