ranges::swap_ranges

ranges::swap_ranges (범위 교환 — ranges)

두 범위의 대응 요소를 서로 교환하는 ranges 버전 알고리즘이에요. <algorithm> 헤더에 있어요.

출처: cppreference

본문

std::ranges::swap_ranges[first1, last1)[first2, last2)(또는 두 번째 범위)의 대응 요소를 교환해요.

namespace std::ranges {
template< std::input_iterator I1, std::sentinel_for<I1> S1,
          std::input_iterator I2, std::sentinel_for<I2> S2 >
requires std::indirectly_swappable<I1, I2>
constexpr swap_ranges_result<I1, I2>
    swap_ranges( I1 first1, S1 last1, I2 first2, S2 last2 );
}
  • 반환 타입 swap_ranges_result{in1, in2}.
std::vector<int> a{1, 2, 3};
std::vector<int> b{9, 8, 7};
std::ranges::swap_ranges(a, b);
// a == {9,8,7}, b == {1,2,3}

두 컨테이너의 대응 요소들을 한 번에 맞바꿀 때 써요. (전체 컨테이너 교환은 멤버 swap이 더 저렴할 수 있어요.)

더 알아보기 (Learn more)

cppreference