Partager via


hash_map::get_allocator

[!REMARQUE]

Cette API est obsolète.l'alternative est unordered_map Class.

Retourne une copie de l'objet d'allocation utilisé pour construire le hash_map.

Allocator get_allocator( ) const;

Valeur de retour

l'allocateur utilisé par le hash_map.

Notes

Les allocateurs de la classe de hash_map spécifient comment la classe gère la mémoire.Les allocateurs par défaut fournis avec les classes de conteneurs STL est suffisant pour la plupart des besoins de programmation.Écrire et utiliser votre propre classe d'un allocateur est rubrique avancée C++.

Dans Visual C++ .NET 2003, les membres des fichiers d'en-tête de <hash_map> et de <hash_set> ne sont plus dans l'espace de noms de DST, mais plutôt ont été déplacés dans l'espace de noms de stdext.Pour plus d'informations, consultez The stdext Namespace.

Exemple

// hash_map_get_allocator.cpp
// compile with: /EHsc
#include <hash_map>
#include <iostream>

int main( )
{
   using namespace std;
   using namespace stdext;
   hash_map <int, int>::allocator_type hm1_Alloc;
   hash_map <int, int>::allocator_type hm2_Alloc;
   hash_map <int, double>::allocator_type hm3_Alloc;
   hash_map <int, int>::allocator_type hm4_Alloc;

   // The following lines declare objects
   // that use the default allocator.
   hash_map <int, int> hm1;
   hash_map <int, int> hm2;
   hash_map <int, double> hm3;

   hm1_Alloc = hm1.get_allocator( );
   hm2_Alloc = hm2.get_allocator( );
   hm3_Alloc = hm3.get_allocator( );

   cout << "The number of integers that can be allocated"
        << endl << "before free memory is exhausted: "
        << hm2.max_size( ) << "." << endl;

   cout << "The number of doubles that can be allocated"
        << endl << "before free memory is exhausted: "
        << hm3.max_size( ) <<  "." << endl;

   // The following line creates a hash_map hm4
   // with the allocator of hash_map hm1.
   hash_map <int, int> hm4( less<int>( ), hm1_Alloc );

   hm4_Alloc = hm4.get_allocator( );

   // Two allocators are interchangeable if
   // storage allocated from each can be
   // deallocated with the other
   if( hm1_Alloc == hm4_Alloc )
   {
      cout << "The allocators are interchangeable."
           << endl;   
   }
   else   
   {
      cout << "The allocators are not interchangeable."
           << endl;   
   }
}

Résultat de l'exemple

La sortie suivante provient pour x86.

The number of integers that can be allocated
before free memory is exhausted: 536870911.
The number of doubles that can be allocated
before free memory is exhausted: 268435455.
The allocators are interchangeable.

Configuration requise

en-tête : <hash_map>

Stdext del'espace de noms :

Voir aussi

Référence

hash_map Class

Modèles Standard