algorithm_iter_swap

algorithm_iter_swap (반복자가 가리키는 값 교환)

std::iter_swap는 두 반복자가 가리키는 요소의 값을 서로 교환해요. swap(*a, *b)와 동등한 효과를 내요.

출처: cppreference

본문

std::iter_swap는 주어진 반복자가 가리키는 요소의 값을 교환해요. <algorithm> 헤더에 정의되어 있어요.

template< class ForwardIt1, class ForwardIt2 >
void iter_swap( ForwardIt1 a, ForwardIt2 b );

다음 조건이 만족되면 동작이 정의되지 않아요:

  • ab가 역참조(dereference) 가능하지 않을 때
  • *a*bSwappable하지 않을 때

복잡도 (Complexity)

상수 시간이에요.

참고 (Notes)

이 함수 템플릿은 사양에서 정의한 swap 연산의 의미를 모델링해요.

예제 (Example)

#include <algorithm>
#include <iostream>
#include <vector>

int main()
{
    std::vector<int> v{1, 2, 3, 4};
    std::iter_swap(v.begin(), v.begin() + 2);
    for (int x : v) std::cout << x << ' ';
    std::cout << '\n';
}

출력:

3 2 1 4

더 알아보기 (Learn more)

cppreference