Espaces de noms
Variantes
Affichages
Actions

std::initializer_list

De cppreference.com
< cpp‎ | utility


 
 
 
std::initializer_list
Les fonctions membres
Original:
Member functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
initializer_list::initializer_list
Capacité
Original:
Capacity
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
initializer_list::size
Les itérateurs
Original:
Iterators
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
initializer_list::begin
initializer_list::end
Tiers fonctions
Original:
Non-member functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
std::begin
std::end
 
Defined in header <initializer_list>
template< class T >
class initializer_list;
(depuis C++11)
Un objet de type std::initializer_list<T> est un objet proxy léger qui permet l'accès à un tableau d'objets de type const T.
Original:
An object of type std::initializer_list<T> is a lightweight proxy object that provides access to an array of objects of type const T.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.


Un object std::initializer_list est automatiquement créé quand:
Original:
A std::initializer_list object is automatically constructed when:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
  • Une liste d'initialisation entre accolades est utilisée, voire list-initialization. Sont également incluses les listes d'initialisation avec les appels de fonction et les expressions d'affectation.
    Original:
    a braced-init-list is used in list-initialization, including function-call list initialization and assignment expressions.
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • Une liste d'initialisation entre accolades est liée avec auto, y compris dans un boucle ranged for
    Original:
    a braced-init-list is bound to auto, including in a ranged for loop
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.


Les objets initializer_list peuvent être implémentés avec une paire de pointeurs, ou avec un pointeur plus longueur de la liste. La copie d'un objet std::initializer_list n'inclut pas la copie des objets sous-jacents.
Original:
Initializer lists may be implemented as a pair of pointers or pointer and length. Copying a std::initializer_list does not copy the underlying objects.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

Sommaire

[modifier] Types de membres

Type du membre Définition
value_type T
reference const T&
const_reference const T&
size_type size_t
iterator const T*
const_iterator const T*

[modifier] Fonctions membres

crée une liste d'initialisation vide
Original:
creates an empty initializer list
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]
Capacité
Original:
Capacity
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
renvoie le nombre d'éléments dans la liste d'initialisation
Original:
returns the number of elements in the initializer list
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]
Les itérateurs
Original:
Iterators
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
renvoie un pointeur du premier élément
Original:
returns a pointer the first element
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]
renvoie un pointeur sur une après le dernier élément
Original:
returns a pointer to one past the last element
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]

[modifier] Fonctions annexes

std::begin spécialisée
Original:
specializes std::begin
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]
std::end spécialisée
Original:
specializes std::end
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]

[modifier] Exemple

#include <iostream>
#include <vector>
#include <initializer_list>
 
template <class T>
struct S {
    std::vector<T> v;
    S(std::initializer_list<T> l) : v(l) {
         std::cout << "constructed with a " << l.size() << "-element list\n";
    }
    void append(std::initializer_list<T> l) {
        v.insert(v.end(), l.begin(), l.end());
    }
    std::pair<const T*, std::size_t> c_arr() const {
        return {&v[0], v.size()};  // copy list-initialization in return statement
                                   // this is NOT a use of std::initializer_list
    }
};
 
template <typename T>
void templated_fn(T) {}
 
int main()
{
    S<int> s = {1, 2, 3, 4, 5}; // copy list-initialization
    s.append({6, 7, 8});      // list-initialization in function call
 
    std::cout << "The vector size is now " << s.c_arr().second << " ints:\n";
 
    for (auto n : s.v)
        std::cout << n << ' ';
    std::cout << '\n';
 
    std::cout << "Range-for over brace-init-list: \n";
 
    for (int x : {-1, -2, -3}) // the rule for auto makes this ranged-for work
        std::cout << x << ' ';
    std::cout << '\n';
 
    auto al = {10, 11, 12};   // special rule for auto
 
    std::cout << "The list bound to auto has size() = " << al.size() << '\n';
 
//    templated_fn({1, 2, 3}); // compiler error! "{1, 2, 3}" is not an expression,
                             // it has no type, and so T cannot be deduced
    templated_fn<std::initializer_list<int>>({1, 2, 3}); // OK
    templated_fn<std::vector<int>>({1, 2, 3});           // also OK
}

Résultat :

constructed with a 5-element list
The vector size is now 8 ints:
1 2 3 4 5 6 7 8
Range-for over brace-init-list: 
-1 -2 -3 
The list bound to auto has size() = 3