Namespaces
Variants

std:: swap (std::array)

From cppreference.net

Défini dans l'en-tête <array>
template < class T, std:: size_t N >

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

std:: array < T, N > & rhs ) ;
(depuis C++11)
(jusqu'à C++17)
template < class T, std:: size_t N >

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

noexcept ( /* voir ci-dessous */ ) ;
(depuis C++17)
(constexpr depuis C++20)
Specializes the std::swap algorithm for std::array . Swaps the contents of lhs and rhs . Calls lhs. swap ( rhs ) .

Cette surcharge participe à la résolution de surcharge seulement si N == 0 ou std:: is_swappable_v < T > est true .

(depuis C++17)

Table des matières

Paramètres

lhs, rhs - conteneurs dont le contenu doit être échangé

Complexité

Linéaire en fonction de la taille des conteneurs.

Exceptions

noexcept spécification :
noexcept ( noexcept ( lhs. swap ( rhs ) ) )
(depuis C++17)

Exemple

#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; };
    // Print state before swap
    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 << "-- SWAP\n";
    std::swap(alice, bob);
    // Print state after swap
    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';
}

Sortie :

Alice: 1 2 3
Bobby: 7 8 9
-- SWAP
Alice: 7 8 9
Bobby: 1 2 3

Voir aussi

échange le contenu
(fonction membre publique)