std:: swap (std::list)
|
Definido en el encabezado
<list>
|
||
|
template
<
class
T,
class
Alloc
>
void
swap
(
std::
list
<
T, Alloc
>
&
lhs,
|
(hasta C++17) | |
|
template
<
class
T,
class
Alloc
>
void
swap
(
std::
list
<
T, Alloc
>
&
lhs,
|
(desde C++17)
(constexpr desde C++26) |
|
Especializa el algoritmo std::swap para std::list . Intercambia los contenidos de lhs y rhs . Llama a lhs. swap ( rhs ) .
ContenidosParámetros
ComplejidadConstante. Excepciones
Ejemplo
Ejecutar este código
#include <algorithm> #include <iostream> #include <list> int main() { std::list<int> alice{1, 2, 3}; std::list<int> bob{7, 8, 9, 10}; auto print = [](const int& n) { std::cout << ' ' << n; }; // Imprimir estado antes del intercambio std::cout << "Alice:"; std::for_each(alice.begin(), alice.end(), print); std::cout << "\nBobby:"; std::for_each(bob.begin(), bob.end(), print); std::cout << '\n'; std::cout << "-- INTERCAMBIO\n"; std::swap(alice, bob); // Imprimir estado después del intercambio std::cout << "Alice:"; std::for_each(alice.begin(), alice.end(), print); std::cout << "\nBobby:"; std::for_each(bob.begin(), bob.end(), print); std::cout << '\n'; } Salida: Alice: 1 2 3 Bobby: 7 8 9 10 -- INTERCAMBIO Alice: 7 8 9 10 Bobby: 1 2 3 Véase también
|