Namespaces
Variants

std:: swap (std::array)

From cppreference.net

Definido en el encabezado <array>
template < class T, std:: size_t N >

void swap ( std:: array < T, N > & lhs,

std:: array < T, N > & rhs ) ;
(desde C++11)
(hasta C++17)
template < class T, std:: size_t N >

void swap ( std:: array < T, N > & lhs,
std:: array < T, N > & rhs )

noexcept ( /* ver más abajo */ ) ;
(desde C++17)
(constexpr desde C++20)
Specializes the std::swap algorithm for std::array . Swaps the contents of lhs and rhs . Calls lhs. swap ( rhs ) .

Esta sobrecarga participa en la resolución de sobrecarga solo si N == 0 o std:: is_swappable_v < T > es true .

(desde C++17)

Contenidos

Parámetros

lhs, rhs - contenedores cuyos contenidos intercambiar

Complejidad

Lineal en tamaño de los contenedores.

Excepciones

noexcept especificación:
noexcept ( noexcept ( lhs. swap ( rhs ) ) )
(desde C++17)

Ejemplo

#include <algorithm>
#include <iostream>
#include <array>
int main()
{
    std::array<int, 3> alice{1, 2, 3};
    std::array<int, 3> bob{7, 8, 9};
    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
-- INTERCAMBIO
Alice: 7 8 9
Bobby: 1 2 3

Véase también

intercambia los contenidos
(función miembro pública)