std :: reverse_copy in C++ STL
C++ STL provides a function that copies the elements from the given range but in reverse order. Below is a simple program to show the working of reverse_copy().
Examples:
Input : 1 2 3 4 5 6 7 8 9 10 Output : The vector is: 10 9 8 7 6 5 4 3 2 1
The function takes three parameters. The first two are the range of the elements which are to be copied and the third parameter is the starting point from where the elements are to be copied in reverse order.
// C++ program to copy from array to vector // using reverse_copy() in STL. #include <bits/stdc++.h> using namespace std; int main() { int src[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; int n = sizeof (src) / sizeof (src[0]); vector< int > dest(n); reverse_copy(src, src + n, dest.begin()); cout << "The vector is: \n" ; for ( int x : dest) { cout << x << " " ; } return 0; } |
The vector is: 10 9 8 7 6 5 4 3 2 1
Below is an example of vector to vector copy.
// C++ program to copy from array to vector // using reverse_copy() in STL. #include <bits/stdc++.h> using namespace std; int main() { vector< int > src { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; vector< int > dest(src.size()); reverse_copy(src.begin(), src.end(), dest.begin()); cout << "The vector is: \n" ; for ( int x : dest) { cout << x << " " ; } return 0; } |
The vector is: 10 9 8 7 6 5 4 3 2 1
Recommended Posts:
- Minimum cells to be flipped to get a 2*2 submatrix with equal elements
- Nested Loops in C++ with Examples
- _Find_first() function in C++ bitset with Examples
- _Find_next() function in C++ bitset with Examples
- Left-Right traversal of all the levels of N-ary tree
- Difference between Iterators and Pointers in C/C++ with Examples
- ostream::seekp(pos) method in C++ with Exmaples
- Default Methods in C++ with Examples
- C++ Tutorial
- Hello World Program : First program while learning Programming
- Difference between Argument and Parameter in C/C++ with Examples
- <cfloat> float.h in C/C++ with Examples
- C/C++ #include directive with Examples
- C/C++ if else statement with Examples
- C/C++ if statement with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.