std::swap_ranges
cppreference.com
<tbody>
</tbody>
template< class ForwardIt1, class ForwardIt2 > ForwardIt2 swap_ranges( ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2 ) |
||
[first1, last1) , first2. .
[first1, last1)
|
||
| 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
.
| , ( ) | |
| ( ) |