I have forgotten

• https://me.yahoo.com

reverse_copy

Copies the elements while reversing their order

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.

Example:

Example - reverse_copy algorithm
Problem
This 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;
}
Solution
Output:

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
References