[ Web Proxy ]
URL:
Viewing: https://ru.cppreference.com/cpp/algorithm/swap_ranges [Back]  [Original]

std::swap_ranges cppreference.com
cppreference.com

std::swap_ranges

cppreference.com
 
C++
(C++20)
(C++20)
(C++11)
(C++20)
/
(C++11)
(C++11)
(C++11)
(C++17)
 
(C++20)
, ranges::copy, ranges::sort, ...
(C++17)
(C++11)(C++11)(C++11)
(C++17)
(C++11)
( )
(C++11)
/
(C++11)
(C++17)

(C++17)
(C++17)
(C++17)
C
 
<tbody> </tbody>
template< class ForwardIt1, class ForwardIt2 > ForwardIt2 swap_ranges( ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2 )

[first1last1) , first2. .

[first1last1)
first2
-
ForwardIt1, ForwardIt2 ForwardIterator.
-
ForwardIt1 ForwardIt2 Swappable

, , , first2.

template<class ForwardIt1, class ForwardIt2>
ForwardIt1 swap_ranges(ForwardIt1 first1,
                             ForwardIt1 last1,
                             ForwardIt2 first2)
{
    while (first1 != last1) {
        std::iter_swap(first1++, first2++);
    }
    return first2;
}

#include <algorithm>
#include <list>
#include <vector>
#include <iostream>
int main()
{
    std::vector<int> v = {1, 2, 3, 4, 5};
    std::list<int> l = {-1, -2, -3, -4, -5};

    std::swap_ranges(v.begin(), v.begin() + 3, l.begin());

    for(int n : v)
       std::cout << n << ' ';
    std::cout << '\n';
    for(int n : l)
       std::cout << n << ' ';
    std::cout << '\n';
}

:

-1 -2 -3 4 5
1 2 3 -4 -5

first last

.

,
( ) []

( ) []

Web Proxy Viewer  |  New URL  |  Original Page