reverse_copy(3C++) - reverse_copy(3C++)
Standard C++ Library Copyright 1998, Rogue Wave Software, Inc.
NAMEreverse_copy
- Reverses the order of elements in a collection while copying them to
a new collection.
SYNOPSIS
#include <algorithm>
template <class BidirectionalIterator, class OutputIterator>
OutputIterator reverse_copy (BidirectionalIterator first,
BidirectionalIterator last,
OutputIterator result);
DESCRIPTION
The reverse_copy algorithm copies the range [first, last) to the range
[result, result + (last - first)) such that for any non- negative inte‐
ger i < (last - first), the following assignment takes place:
*(result + (last - first) -i) = *(first + i)
reverse_copy returns result + (last - first). The ranges [first, last)
and [result, result + (last - first)) must not overlap.
COMPLEXITYreverse_copy performs exactly (last - first) assignments.
EXAMPLE
//
// reverse.cpp
//
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
int main ()
{
//
// Initialize a vector with an array of integers.
//
int arr[10] = { 1,2,3,4,5,6,7,8,9,10 };
vector<int> v(arr+0, arr+10);
//
// Print out elements in original (sorted) order.
//
cout << "Elements before reverse: " << endl << " ";
copy(v.begin(), v.end(),
ostream_iterator<int,char>(cout," "));
cout << endl << endl;
//
// Reverse the ordering.
//
reverse(v.begin(), v.end());
//
// Print out the reversed elements.
//
cout << "Elements after reverse: " << endl << " ";
copy(v.begin(), v.end(),
ostream_iterator<int,char>(cout," "));
cout << endl << endl;
cout << "A reverse_copy to cout: " << endl << " ";
reverse_copy(v.begin(), v.end(), <br> ostream_itera‐
tor<int,char>(cout, " "));
cout << endl;
return 0;
}
Program OutputElements before reverse:
1 2 3 4 5 6 7 8 9 10
Elements after reverse:
10 9 8 7 6 5 4 3 2 1
A reverse_copy to cout:
1 2 3 4 5 6 7 8 9 10
WARNINGS
If your compiler does not support default template parameters, then you
always need to supply the Allocator template argument. For instance,
you need to write:
vector<int, allocator<int> >
instead of:
vector<int>
If your compiler does not support namespaces, then you do not need the
using declaration for std.
SEE ALSO
reverse
Rogue Wave Software 02 Apr 1998 reverse_copy(3C++)