Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/algorithm/swap ranges"

From cppreference.com
< cpp‎ | algorithm
(+type reqs)
m (+reqs)
Line 17: Line 17:
 
{{param list hreq}}
 
{{param list hreq}}
 
{{param list req concept | ForwardIt1, ForwardIt2 | ForwardIterator}}
 
{{param list req concept | ForwardIt1, ForwardIt2 | ForwardIterator}}
 +
{{param list req concept | *(first1+n), *(first2+n) | Swappable}}
 
{{param list end}}
 
{{param list end}}
  

Revision as of 04:53, 29 August 2012

 
 
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
(C++11)                (C++11)(C++11)

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
(C++11)
(C++17)
Lexicographical comparison operations
Permutation operations
C library
Numeric operations
Operations on uninitialized memory
 

Template:ddcl list begin <tr class="t-dsc-header">

<td>
Defined in header <algorithm>
</td>

<td></td> <td></td> </tr> <tr class="t-dcl ">

<td class="t-dcl-nopad">
template< class ForwardIt1, class ForwardIt2 >
ForwardIt2 swap_ranges( ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2 )
</td>

<td class="t-dcl-nopad"> </td> <td class="t-dcl-nopad"> </td> </tr> Template:ddcl list end

Exchanges elements between range [first1, last1) and another range starting at first2.

Contents

Parameters

first1, last1 - the first range of elements to swap
first2 - beginning of the second range of elements to swap
Type requirements
-
ForwardIt1, ForwardIt2 must meet the requirements of LegacyForwardIterator.
-
  • (first1+n), *(first2+n) must meet the requirements of Swappable.

Return value

Iterator to the element past the last element exchanged in the range beginning with first2.

Possible implementation

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

Example

Demonstrates swapping of subranges from different containers

#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';
}

Output:

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

Complexity

linear in the distance between first and last

See also

Template:cpp/algorithm/dcl list iter swapTemplate:cpp/algorithm/dcl list swap