std::swap_ranges

From Cppreference

Jump to: navigation, search
Defined in header <algorithm>

template< class ForwardIterator1, class ForwardIterator2 >

ForwardIterator2 swap_ranges( ForwardIterator1 first1,
                              ForwardIterator1 last1,

                              ForwardIterator2 first2 )

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

Return value

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


Equivalent function

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

Example

Complexity

linear in the distance between first and last

See also

iter_swap
swaps the elements pointed to by two iterators
(function template)
swap
swaps the values of two objects
(function template)
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox
In other languages