◐ Shell
clean mode source ↗

std::array — cppreference.com

De cppreference.com

<tbody> </tbody>

Déclaré dans l'en-tête

<array>

template< class T, std::size_t N > struct array;

(depuis C++11)

std::array est un conteneur qui encapsule des tableaux de taille constante (connue à la compilation).

Cette structure possède la même sémantique de type agrégat qu'un tableau de style C. La taille et l'efficacité de array<T,N> pour un certain nombre d'éléments sont équivalentes à celles du tableau T[N] de style C correspondant. La structure offre les avantages d'un conteneur standard, telles que la connaissance du nombre d'éléments contenus, le support de l'affectation, les itérateurs à accès aléatoire, etc.

Il existe un cas particulier pour un std::array de longueur zéro (N == 0). Dans ce cas, array.begin() == array.end(), qui est une valeur unique. L'effet de l'appel de front() ou back() sur un tableau de taille zéro est indéfini .

Array est un agrégat (il n'a pas de constructeurs et aucun membre en privé ou protégé), ce qui lui permet d'utiliser agrégat d'initialisation.

Un tableau peut aussi être utilisé comme un n-uplet de N éléments du même type.

Types membres

Type membre Definition
value_type T [edit]
size_type size_t [edit]
difference_type ptrdiff_t [edit]
reference value_type& [edit]
const_reference const value_type& [edit]
pointer T*[edit]
const_pointer const T*[edit]
iterator RandomAccessIterator [edit]
const_iterator Itérateur constant à accès aléatoire [edit]
reverse_iterator std::reverse_iterator<iterator> [edit]
const_reverse_iterator std::reverse_iterator<const_iterator> [edit]

Fonctions membres

Accès aux éléments
accède à l'élément spécifié avec vérification de bornes
(fonction membre publique) [edit]
accède à l'élément spécifié
(fonction membre publique) [edit]
accède au premier élément
(fonction membre publique) [edit]
accède au dernier élément
(fonction membre publique) [edit]

(C++11)

accède directement au tableau sous-jacent
(fonction membre publique) [edit]
Itérateurs

retourne un itérateur au début

Original:

returns an iterator to the beginning

The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.


(fonction membre publique) [edit]

retourne un itérateur à la fin

Original:

returns an iterator to the end

The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.


(fonction membre publique) [edit]
retourne un itérateur inversé au début
(fonction membre publique) [edit]
retourne un itérateur inversé à la fin
(fonction membre publique) [edit]
Capacité

vérifie si le conteneur est vide

Original:

checks whether the container is empty

The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.


(fonction membre publique) [edit]

retourne le nombre d'éléments

Original:

returns the number of elements

The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.


(fonction membre publique) [edit]

retourne le plus grand nombre possible d'éléments

Original:

returns the maximum possible number of elements

The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.


(fonction membre publique) [edit]
Opérations

remplir le récipient avec la valeur spécifiée

Original:

fill the container with specified value

The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.


(fonction membre publique) [edit]

permute les contenus

Original:

swaps the contents

The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.


(fonction membre publique) [edit]

Fonctions tiers

compare lexicographiquement les valeurs dans le array

Original:

lexicographically compares the values in the array

The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.


(fonction générique) [edit]
accesses an element of an array
(fonction générique) [edit]

l'algorithme spécialisé std::swap

Original:

specializes the std::swap algorithm

The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.


(fonction générique) [edit]

Classes d'aide

obtient la taille d'une array

Original:

obtains the size of an array

The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.


(classe générique spécialisée) [edit]

obtient le type des éléments de array

Original:

obtains the type of the elements of array

The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.


(classe générique spécialisée) [edit]

Exemple

#include <string>
#include <iterator>
#include <iostream>
#include <algorithm>
#include <array>

int main()
{
    // La construction utilise l'aggrégat d'initialisation
    std::array<int, 3> a1{ {1,2,3} };    // Double accolades requises
    std::array<int, 3> a2 = {1, 2, 3}; // sauf après un =
    std::array<std::string, 2> a3 = { {std::string("a"), "b"} };

    // Les opérations sur les conteneurs sont supportées
    std::sort(a1.begin(), a1.end());
    std::reverse_copy(a2.begin(), a2.end(), std::ostream_iterator<int>(std::cout, " "));

    // La boucle "ranged for" est supportée
    for(auto& s: a3)
        std::cout << s << ' ';
}