This C++ program demonstrates the reverse() algorithm. The function reverse() takes two iterators as parameters and reverses the order of the elements of the container. The program creates a string array and reverses the order of the strings.
Here is the source code of the C++ program which demonstrates the reverse() 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() algorithm
*/
#include <iostream>
#include <algorithm>
#include <vector>
#include <iomanip>
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"};
cout << "Original order : ";
print(s, 6);
cout << "Reversing the order ... " << endl;
reverse(s, s + 6);
cout << "Reversed order : ";
print(s, 6);
}
$ a.out Original order : 1. George 2. John 3. Lucy 4. Alice 5. Bob 6. Watson Reversing the order ... Reversed order : 1. Watson 2. Bob 3. Alice 4. Lucy 5. John 6. George
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:
- Practice Programming MCQs
- Check Programming Books
- Check Computer Science Books
- Check C++ Books
- Practice Computer Science MCQs