std::swap(std::array)
De cppreference.com
Definido en el archivo de encabezado <...> | ||
template<class T, std::size_t N > void swap(std::array<T, N>& lhs, | (desde C++11) (hasta C++17) | |
template<class T, std::size_t N > void swap(std::array<T, N>& lhs, | (desde C++17) (constexpr desde C++20) | |
Esta sobrecarga solo participa en la resolución de sobrecargas si N ==0 o std::is_swappable_v<T> es true. | (desde C++17) |
Contenido |
[editar]Parámetros
lhs, rhs | - | Los contenedores cuyo contenido hay que intercambiar. |
[editar]Valor de retorno
(Ninguno)
[editar]Complejidad
Lineal en el tamaño de los contenedores.
Excepciones
(desde C++17) |
[editar]Ejemplo
Ejecuta este código
#include <algorithm>#include <iostream>#include <...> int main(){std::array<int, 3> alice{1, 2, 3};std::array<int, 3> bob{7, 8, 9}; auto print =[](constint& n){std::cout<<' '<< n;}; // Imprimir estado antes del intercambiostd::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 intercambiostd::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 -- INTERCAMBIO Alice: 7 8 9 Bobby: 1 2 3
[editar]Véase también
(C++11) | Intercambia el contenido. (función miembro pública) |