Copies the elements while reversing their order
You're viewing an older version of this page (#4152). View the current version.
View versions (1)
Definition
The reverse_copy() algorithm is defined in the standard header <algorithm> and in the nonstandard backward-compatibility header <algo.h>.
Interface
template < class BidirectionalIterator, class OutputIterator >
OutputIterator reverse_copy(
BidirectionalIterator first,
BidirectionalIterator last,
OutputIterator result
);
Parameters:
| Parameter | Description |
| first | A bidirectional iterator pointing to the position of the first element in the source range within which the elements are being permuted |
| last | A bidirectional iterator pointing to the position one past the final element in the source range within which the elements are being permuted |
| result | An output iterator pointing to the position of the first element in the destination range to which elements are being copied |
Description
Reverse_copy copies the elements from the range [first, last), to another range beginning at result in such a way, that the elements in the new range are in reverse order.
Return Value
Returns an iterator on the element following the last element copied.
Complexity
The complexity is linear; performs last - first assignments.
References
Blank
Example 1
ProblemThis program illustrates the functionality of reverse_copy() algorithm.
Workings#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
int main()
{
vector <int> vec1, vec2(11);
vector <int>::iterator Iter1, Iter2;
int i;
for (i = 10; i <= 20; i++)
vec1.push_back(i);
cout <<"The original vector vec1 data is:\n";
for (Iter1 = vec1.begin(); Iter1 != vec1.end(); Iter1++)
cout <<*Iter1<<" ";
cout <<endl;
// reverse the elements in the vector
reverse_copy(vec1.begin(), vec1.end(), vec2.begin());
cout <<"The copy vec2 data of the reversed vector vec1 is:\n";
for (Iter2 = vec2.begin(); Iter2 != vec2.end(); Iter2++)
cout <<*Iter2<<" ";
cout <<endl;
cout <<"The original vector vec1 unmodified as:\n";
for (Iter1 = vec1.begin(); Iter1 != vec1.end(); Iter1++)
cout <<*Iter1<<" ";
cout <<endl;
return 0;
}
SolutionOutput:
The original vector vec1 data is:
10 11 12 13 14 15 16 17 18 19 20
The copy vec2 data of the reversed vector vec1 is:
20 19 18 17 16 15 14 13 12 11 10
The original vector vec1 unmodified as:
10 11 12 13 14 15 16 17 18 19 20
Example 2
ProblemThe following program demonstrates how to use reverse() and reverse_copy().
Workings#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
using namespace std;
int main()
{
vector<int> coll;
INSERT_ELEMENTS(coll,1,9);
PRINT_ELEMENTS(coll,"coll: ");
// reverse order of elements
reverse (coll.begin(), coll.end());
PRINT_ELEMENTS(coll,"coll: ");
// reverse order from second to last element but one
reverse (coll.begin()+l, coll.end()-l);
PRINT_ELEMENTS(coll,"coll: ");
//print all of them in reverse order
reverse_copy (coll.begin(), coll.end(), // source
ostream_iterator<int>(cout," ")); // destination
cout <<endl;
return 0;
}
SolutionOutput:
coll: 1 2 3 4 5 6 7 8 9
coll: 9 8 7 6 5 4 3 2 1
coll: 9 2 3 4 5 6 7 8 1
1 8 7 6 5 4 3 2 9
References- Nicolai M. Josuttis: "The C++ Standard Library"
See Also