Namespaces
Variants

std:: swap_ranges

From cppreference.net
Algorithm library
Constrained algorithms and algorithms on ranges (C++20)
Constrained algorithms, e.g. ranges::copy , ranges::sort , ...
Execution policies (C++17)
Non-modifying sequence operations
Batch operations
(C++17)
Search operations
Modifying sequence operations
Copy operations
(C++11)
(C++11)
Swap operations
swap_ranges

Transformation operations
Generation operations
Removing operations
Order-changing operations
(until C++17) (C++11)
(C++20) (C++20)
Sampling operations
(C++17)

Sorting and related operations
Partitioning operations
Sorting operations
Binary search operations
(on partitioned ranges)
Set operations (on sorted ranges)
Merge operations (on sorted ranges)
Heap operations
Minimum/maximum operations
Lexicographical comparison operations
Permutation operations
C library
Numeric operations
Operations on uninitialized memory
헤더 파일에 정의됨 <algorithm>
template < class ForwardIt1, class ForwardIt2 >

ForwardIt2 swap_ranges ( ForwardIt1 first1, ForwardIt1 last1,

ForwardIt2 first2 ) ;
(1) (C++20부터 constexpr)
template < class ExecutionPolicy,

class ForwardIt1, class ForwardIt2 >
ForwardIt2 swap_ranges ( ExecutionPolicy && policy,
ForwardIt1 first1, ForwardIt1 last1,

ForwardIt2 first2 ) ;
(2) (C++17부터)
1) 범위 [ first1 , last1 ) std:: distance ( first1, last1 ) 개의 요소를 가진 first2 에서 시작하는 다른 범위 사이의 요소들을 교환합니다.
2) (1) 과 동일하지만, policy 에 따라 실행됩니다.
이 오버로드는 다음의 모든 조건이 만족될 때만 오버로드 해결에 참여합니다:

std:: is_execution_policy_v < std:: decay_t < ExecutionPolicy >> true 인 경우.

(C++20 이전)

std:: is_execution_policy_v < std:: remove_cvref_t < ExecutionPolicy >> true 인 경우.

(C++20 이후)

다음 조건 중 하나라도 충족되면, 동작은 정의되지 않습니다:

  • 두 범위가 겹칩니다.
  • 두 범위에 상응하는 반복자 iter1 iter2 가 존재하여 * iter1 Swappable 하지 않습니다. * iter2

목차

매개변수

first1, last1 - 교환할 요소들의 범위 를 정의하는 반복자 쌍
first2 - 교환할 두 번째 요소 범위의 시작점
policy - 사용할 실행 정책
타입 요구사항
-
ForwardIt1, ForwardIt2 LegacyForwardIterator 요구사항을 충족해야 함

반환값

first2 로 시작하는 범위에서 교환된 마지막 요소의 다음 요소를 가리키는 반복자.

복잡도

정확히 std:: distance ( first1, last1 ) 번의 교환을 수행합니다.

예외

ExecutionPolicy 라는 템플릿 매개변수를 사용하는 오버로드는 다음과 같이 오류를 보고합니다:

  • 알고리즘의 일부로 호출된 함수 실행 중 예외가 발생하고 ExecutionPolicy 표준 정책 중 하나인 경우, std::terminate 가 호출됩니다. 다른 ExecutionPolicy 의 경우 동작은 구현에 따라 정의됩니다.
  • 알고리즘이 메모리 할당에 실패할 경우, std::bad_alloc 이 throw됩니다.

참고 사항

구현체들(예: MSVC STL )은 반복자 타입이 LegacyContiguousIterator 요구사항을 만족하고, 그 값 타입의 스왑 작업이 비트리비얼 특수 멤버 함수나 ADL 에서 발견된 swap 을 호출하지 않을 때 벡터화를 활성화할 수 있습니다.

가능한 구현

template<class ForwardIt1, class ForwardIt2>
constexpr //< since C++20
ForwardIt2 swap_ranges(ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2)
{
    for (; first1 != last1; ++first1, ++first2)
        std::iter_swap(first1, first2);
    return first2;
}

예제

서로 다른 컨테이너의 부분 범위 교환을 보여줍니다.

#include <algorithm>
#include <iostream>
#include <list>
#include <vector>
auto print = [](auto comment, auto const& seq)
{
    std::cout << comment;
    for (const auto& e : seq)
        std::cout << e << ' ';
    std::cout << '\n';
};
int main()
{
    std::vector<char> v{'a', 'b', 'c', 'd', 'e'};
    std::list<char> l{'1', '2', '3', '4', '5'};
    print("Before swap_ranges:\n" "v: ", v);
    print("l: ", l);
    std::swap_ranges(v.begin(), v.begin() + 3, l.begin());
    print("After swap_ranges:\n" "v: ", v);
    print("l: ", l);
}

출력:

Before swap_ranges:
v: a b c d e
l: 1 2 3 4 5
After swap_ranges:
v: 1 2 3 d e
l: a b c 4 5

참고 항목

두 반복자가 가리키는 요소를 교환
(함수 템플릿)
두 객체의 값을 교환
(함수 템플릿)
두 요소 범위를 교환
(알고리즘 함수 객체)