Bibliothèque de listes C ++ - fonction get_allocator ()

La description

La fonction C ++ std::list::get_allocator() renvoie un allocateur associé à list.

Déclaration

Voici la déclaration de la fonction std :: list :: get_allocator () sous forme d'en-tête std :: list.

C ++ 98

allocator_type get_allocator() const;

C ++ 11

allocator_type get_allocator() const noexcept;

Paramètres

Aucun

Valeur de retour

Renvoie un allocateur associé à la liste.

Exceptions

Cette fonction membre ne lève jamais d'exception.

Complexité temporelle

Constante ie O (1)

Exemple

L'exemple suivant montre l'utilisation de la fonction std :: list :: get_allocator ().

#include <iostream>
#include <list>

using namespace std;

int main(void) {
   list<int> l = {1, 2, 3, 4, 5};
   int *p = NULL;

   p = l.get_allocator().allocate(5);

   for (int i = 0; i < 5; ++i)
      p[i] = i + 1;

   cout << "List contains following elements" << endl;

   for (int i = 0; i < 5; ++i)
      cout << p[i] << endl;

   return 0;
}

Compilons et exécutons le programme ci-dessus, cela produira le résultat suivant -

List contains following elements
1
2
3
4
5