Compartir a través de


hash_map::erase

Nota

Esta API está obsoleta.La alternativa es unordered_map (Clase).

Quita un elemento o un intervalo de elementos en un hash_map de posiciones especificadas o quita los elementos que coinciden con una clave especificada.

iterator erase( 
   iterator _Where 
); 
iterator erase( 
   iterator _First, 
   iterator _Last 
); 
size_type erase( 
   const key_type& _Key 
);

Parámetros

  • _Where
    Posición del elemento que se va a quitar del hash_map.

  • _First
    Posición del primer elemento quitado de hash_map.

  • _Last
    Colocar simplemente más allá del último elemento quitado de hash_map.

  • _Key
    El valor de clave de los elementos que se van a quitar de hash_map.

Valor devuelto

Para las primeras dos funciones miembro, un iterador bidireccional que designa el primer elemento que permanece más allá de cualquier elemento quitado, o un puntero al final de hash_map si no existe ese elemento.

Para la tercera función miembro, devuelve el número de elementos que se han quitado de hash_map.

Comentarios

Las funciones miembro nunca producen una excepción.

En Visual C++ .NET 2003, los miembros de los archivos de encabezado <hash_map> y <hash_set> ya no están en el espacio de nombres std, sino que se han movido al espacio de nombres stdext. Vea El espacio de nombres stdext para obtener más información.

Ejemplo

Al compilar este ejemplo con el marcador de /Wp64 o en una plataforma de 64 bits, el compilador que excluya C4267 se generará. Para obtener más información sobre esta advertencia, vea Advertencia del compilador (nivel 3) C4267.

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

int main()
{
    using namespace std;
    using namespace stdext;
    hash_map<int, int> hm1, hm2, hm3;
    hash_map<int, int> :: iterator pIter, Iter1, Iter2;
    int i;
    hash_map<int, int>::size_type n;
    typedef pair<int, int> Int_Pair;

    for (i = 1; i < 5; i++)
    {
        hm1.insert(Int_Pair (i, i));
        hm2.insert(Int_Pair (i, i*i));
        hm3.insert(Int_Pair (i, i-1));
    }

    // The 1st member function removes an element at a given position
    Iter1 = ++hm1.begin();
    hm1.erase(Iter1);

    cout << "After the 2nd element is deleted, the hash_map hm1 is:";
    for (pIter = hm1.begin(); pIter != hm1.end(); pIter++)
        cout << " " << pIter -> second;
    cout << "." << endl;

    // The 2nd member function removes elements
    // in the range [_First, _Last)
    Iter1 = ++hm2.begin();
    Iter2 = --hm2.end();
    hm2.erase(Iter1, Iter2);

    cout << "After the middle two elements are deleted, "
         << "the hash_map hm2 is:";
    for (pIter = hm2.begin(); pIter != hm2.end(); pIter++)
        cout << " " << pIter -> second;
    cout << "." << endl;

    // The 3rd member function removes elements with a given _Key
    n = hm3.erase(2);

    cout << "After the element with a key of 2 is deleted,\n"
         << "the hash_map hm3 is:";
    for (pIter = hm3.begin(); pIter != hm3.end(); pIter++)
        cout << " " << pIter -> second;
    cout << "." << endl;

    // The 3rd member function returns the number of elements removed
    cout << "The number of elements removed from hm3 is: "
         << n << "." << endl;

    // The dereferenced iterator can also be used to specify a key
    Iter1 = ++hm3.begin();
    hm3.erase(Iter1);

    cout << "After another element with a key equal to that"
         << endl;
    cout  << "of the 2nd element is deleted, "
          << "the hash_map hm3 is:";
    for (pIter = hm3.begin(); pIter != hm3.end(); pIter++)
        cout << " " << pIter -> second;
    cout << "." << endl;
}
  

Requisitos

Encabezado: <hash_map>

Espacio de nombres: stdext

Vea también

Referencia

hash_map (Clase)

Biblioteca de plantillas estándar