next up previous index
Next: Transform a sequence Up: Mutating sequence operations Previous: Copy one sequence

Exchange values or ranges

Source code

Declaration

template <class T>
inline void swap(T& a, T& b);

template <class ForwardIterator1, class ForwardIterator2>
inline void iter_swap(ForwardIterator1 a, ForwardIterator2 b);

template <class ForwardIterator1, class ForwardIterator2>
ForwardIterator2 swap_ranges(ForwardIterator1 first1, ForwardIterator1 last1,
                             ForwardIterator2 first2);

Description      The swap  function swaps   two elements.

The iter_swap  function swaps   the contents of two iterators.

The swap_ranges  function exchangesexchange!two sequences  the the first n elements of the sequence in the range [ first1, last1) with those in the range of size n beginning at first2 , where n is the size of the range [ first, last) , and returns the past-the-end location  of the range of size n, where n is the size of the range [ first, last) , beginning at first2.

Type requirements

Group Mutating sequence operations.

Time complexity Constant for swap  and iter_swap , and linear for swap_ranges .

For swap_ranges , the number of swaps performed is the size of the range [ first1, last1) .

Space complexity Constant.

Mutative? Yes.

Example Select here to view the source code for the example.

 

Implementation Select here to view source code (Copyright (C) 1994, Hewlett-Packard Company) for this algorithm.



Kenny Zalewski
Mon May 13 04:03:40 EDT 1996