|
| |
|
| Category: algorithms | | Component type: function |
template <class BidirectionalIterator>
void reverse(BidirectionalIterator first, BidirectionalIterator last);
Reverse reverses a range. That is: for every
i such that
0 <= i <= (last - first) / 2), it exchanges
*(first + i) and
*(last - (i + 1)).
Defined in the standard header
algorithm, and in the nonstandard backward-compatibility header
algo.h.
-
[first, last) is a valid range.
Linear:
reverse(first, last) makes
(last - first) / 2 calls to
swap.
Vector<int> V;
V.push_back(0);
V.push_back(1);
V.push_back(2);
copy(V.begin(), V.end(), ostream_iterator<int>(cout, " "));
reverse(V.begin(), V.end());
copy(V.begin(), V.end(), ostream_iterator<int>(cout, " "));
reverse_copy