forward_list_swap

forward_list_swap (std::forward_list::swap — 내용 교환)

std::forward_list 하나의 내용을 다른 객체 other와 맞바꾸는 멤버 함수예요. 개별 원소에 대해 이동·복사·교환 연산을 호출하지 않아요.

출처: cppreference

본문

시그니처는 다음과 같아요.

void swap( forward_list& other );                     // (since C++11) (until C++17)
void swap( forward_list& other ) noexcept(/* see below */);   // (since C++17)

컨테이너의 내용을 other의 내용과 교환해요. 개별 원소에 대해 어떤 이동·복사·교환 연산도 호출하지 않아요. 모든 이터레이터와 참조는 유효하게 남아요. 이 컨테이너에서 end() 값을 가리키던 이터레이터가 연산 후 이 컨테이너를 가리킬지 other를 가리킬지는 지정되지 않아요.

C++17부터는 std::allocator_traits<allocator_type>::propagate_on_container_swap이 유효한지 여부에 따라 noexcept 여부가 결정돼요.

매개변수

  • other: 내용을 교환할 컨테이너

복잡도

상수(constant)예요.

예제

#include <forward_list>
#include <iostream>
int main()
{
    std::forward_list<int> a{1, 2};
    std::forward_list<int> b{3, 4};
    a.swap(b);
    for (int x : a) std::cout << x << ' ';  // 3 4
}

함께 보기

  • std::swap(std::forward_list): std::swap 알고리즘을 특수화해요

더 알아보기 (Learn more)

cppreference