next up previous index
Next: Rotate elements in Up: Mutating sequence operations Previous: Remove consecutive duplicates

Reverse a sequence

Source code

Declaration

template <class BidirectionalIterator>
inline void reverse(BidirectionalIterator first, BidirectionalIterator last);

template <class BidirectionalIterator, class OutputIterator>
OutputIterator reverse_copy(BidirectionalIterator first,
                            BidirectionalIterator last, OutputIterator result);

Description  The function reverse  reverses  the order of the elements in range [ first, last) .

The function reverse_copy  reverses  the order of the elements in range [ first, last) , placing the result in the range of size n beginning at result , where n is the size of the range [ first, last) , and returns the location l that is the past-the-end iteratoriterator!past-the-end for the resulting sequence.

Type requirements

Group Mutating sequence operations.

Time complexity Linear.

For reverse , exactly

element exchanges are performed, where n is the size of the range [ first, last) .

For reverse_copy , exactly n assignment operations are performed, where n is the size of the range [ first, last) .

Space complexity Constant.

Mutative? Yes.

Details For the sequence in the range [ first, last) , the resulting sequence is .

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