This C++ program demonstrates the reverse_copy() algorithm. The function reverse_copy() takes three iterators as parameters – two iterators to the beginning and the end of the container elements to be reversed and the third iterator to the container where reversed elements are to be saved. The program demonstrates the use of the algorithm which doesn’t modify the order of the container whose order is being reversed.
Here is the source code of the C++ program which demonstrates the reverse_copy() algorithm. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C++ Program to reverse the order of elements using reverse_copy() algorithm
*/
#include <iostream>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <iterator>
using namespace std;
void print(string a[], int N)
{
for(int i = 0; i < N; i++)
{
cout << (i + 1) << ". " << setw(5)
<< a[i] << " ";
}
cout << endl;
}
int main()
{
string s[] = {"George", "John", "Lucy", "Alice", "Bob", "Watson"};
string t[6];
cout << "Original order : ";
print(s, 6);
cout << "Reversing the order ... " << endl;
// Doesn't modify original array s[]
reverse_copy(s, s + 6, t);
cout << "Original order : ";
print(s, 6);
cout << "Reversed order : ";
print(t, 6);
}
$ a.out Original order : 1. George 2. John 3. Lucy 4. Alice 5. Bob 6. Watson Reversing the order ... Original order : 1. George 2. John 3. Lucy 4. Alice 5. Bob 6. Watson Reversed order : 1. Watson 2. Bob 3. Alice 4. Lucy 5. John 6. George 4
Sanfoundry Global Education & Learning Series – 1000 C++ Programs.
advertisement
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.
Related Posts:
- Check Computer Science Books
- Apply for Information Technology Internship
- Check Programming Books
- Practice Computer Science MCQs
- Check C++ Books