Namespaces
Variants

std:: swap (std::array)

From cppreference.net

헤더 파일에 정의됨 <array>
template < class T, std:: size_t N >

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

std:: array < T, N > & rhs ) ;
(C++11부터)
(C++17까지)
template < class T, std:: size_t N >

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

noexcept ( /* see below */ ) ;
(C++17부터)
(C++20부터 constexpr)
Specializes the std::swap algorithm for std::array . Swaps the contents of lhs and rhs . Calls lhs. swap ( rhs ) .

이 오버로드는 다음 조건에서만 오버로드 해결에 참여합니다: N == 0 또는 std:: is_swappable_v < T > true 인 경우.

(C++17부터)

목차

매개변수

lhs, rhs - 내용을 교환할 컨테이너들

복잡도

컨테이너 크기에 선형적으로 비례합니다.

예외

noexcept 명세:
noexcept ( noexcept ( lhs. swap ( rhs ) ) )
(C++17부터)

예제

#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; };
    // 스왑 전 상태 출력
    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);
    // 스왑 후 상태 출력
    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';
}

출력:

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

참고 항목

내용을 교환합니다
(public member function)