swap (Función) (unordered_map)

Intercambia el contenido de dos contenedores.

template<class Key, class Ty, class Hash, class Pred, class Alloc>
    void swap(
        unordered_map <Key, Ty, Hash, Pred, Alloc>& left,
        unordered_map <Key, Ty, Hash, Pred, Alloc>& right);

Parámetros

  • Key
    El tipo de clave.

  • Ty
    El tipo asignado.

  • Hash
    El tipo de objeto de la función hash.

  • Pred
    El tipo de objeto de función de comparación de igualdad.

  • Alloc
    La clase de asignador.

  • left
    El primer contenedor a cambiar.

  • right
    El segundo contenedor a cambiar.

Comentarios

La función de la plantilla se ejecuta left.unordered_map::swap(right).

Ejemplo

 

// std_tr1__unordered_map__u_m_swap.cpp 
// compile with: /EHsc 
#include <unordered_map> 
#include <iostream> 
 
typedef std::unordered_map<char, int> Mymap; 
int main() 
    { 
    Mymap c1; 
 
    c1.insert(Mymap::value_type('a', 1)); 
    c1.insert(Mymap::value_type('b', 2)); 
    c1.insert(Mymap::value_type('c', 3)); 
 
// display contents " [c 3] [b 2] [a 1]" 
    for (Mymap::const_iterator it = c1.begin(); 
        it != c1.end(); ++it) 
        std::cout << " [" << it->first << ", " << it->second << "]"; 
    std::cout << std::endl; 
 
    Mymap c2; 
 
    c2.insert(Mymap::value_type('d', 4)); 
    c2.insert(Mymap::value_type('e', 5)); 
    c2.insert(Mymap::value_type('f', 6)); 
 
    c1.swap(c2); 
 
// display contents " [f 6] [e 5] [d 4]" 
    for (Mymap::const_iterator it = c1.begin(); 
        it != c1.end(); ++it) 
        std::cout << " [" << it->first << ", " << it->second << "]"; 
    std::cout << std::endl; 
 
    swap(c1, c2); 
 
// display contents " [c 3] [b 2] [a 1]" 
    for (Mymap::const_iterator it = c1.begin(); 
        it != c1.end(); ++it) 
        std::cout << " [" << it->first << ", " << it->second << "]"; 
    std::cout << std::endl; 
 
    return (0); 
    } 
 
  

Requisitos

Encabezado: <unordered_map>

Espacio de nombres: std

Vea también

Referencia

<unordered_map>

unordered_map::swap

Otros recursos

miembros de <unordered_map>