Vectors are similar to dynamic arrays in that they can resize themselves when an element is added or removed, and the container takes care of their storage. Iterators can access and traverse vector elements since they’re stored in contiguous storage. Data is inserted at the top of vectors. Inserting at the end takes longer since the array may need to be extended at times. There is no resizing, and removing the final variable still takes the same amount of time. Inserting and erasing at the start or within the middle is linear in terms of your time .
Given a vector, the task is to print the vector in reverse order
Examples:
Input:
vector ={ 1, 3, 4, 7, 8, 9 }
Output:
9 8 7 4 3 1
Display a Vector in Reverse Order
There are several ways to print the vector in reverse order some of them are:
- Using reverse iterator (end() and begin())
- Using reverse iterator( rbegin() and rend())
- Using indexing
- Using copy() function to print in one line
Method #1:Using reverse iterator (end() and begin())
We run a iterator from end of the vector to beginning of the vector.
Below is the implementation:
#include <bits/stdc++.h> using namespace std; int main() { // creating a vector with some values vector<int> intvect{ 1, 3, 4, 7, 8, 9 }; // reverse iterating the vector using for loop for (auto itr = intvect.end() - 1;itr != intvect.begin() - 1; itr--) cout << *itr << " "; return 0; }
Output:
9 8 7 4 3 1
Method #2:Using reverse iterator( rbegin() and rend())
Vector has two member functions in C++ that return a reverse iterator.
rbegin(): Returns a reverse iterator that points to the vector’s last element.
rend() : Returns a reverse iterator pointing to the element preceding the vector’s first element (theoriticaly).
We can iterate over the vector elements in reverse order using the reverse iterators returned by rbegin() and rend() and print them one by one.
Below is the implementation:
#include <bits/stdc++.h> using namespace std; int main() { // creating a vector with some values vector<int> intvect{ 1, 3, 4, 7, 8, 9 }; // reverse iterating the vector using for loop for (auto itr = intvect.rbegin(); itr != intvect.rend(); itr++) cout << *itr << " "; return 0; }
Output:
9 8 7 4 3 1
Method #3:Using indexing
If the reverse iterator is not to be used, then you can use indexing to iterate and print them one by one across all elements of your vector in reverse order.
Below is the implementation:
#include <bits/stdc++.h> using namespace std; int main() { // creating a vector with some values vector<int> intvect{ 1, 3, 4, 7, 8, 9 }; // using indexing tp print in reverse order for (int value = intvect.size() - 1; value >= 0; value--) { // print the elements cout << intvect[value] << " "; } return 0; }
Output:
9 8 7 4 3 1
Method #4:Using copy() function to print in one line
We can pull all elements between vector end and vector start to the output stream using the STL algorithm copy() using the reverse iterators provided by rbegin() and rend() .
Below is the implementation:
#include <bits/stdc++.h> using namespace std; int main() { // creating a vector with some values vector<int> intvect{ 1, 3, 4, 7, 8, 9 }; // using copy() function copy(intvect.rbegin(), intvect.rend(), ostream_iterator<int>(cout, " ")); return 0; }
Output:
9 8 7 4 3 1
Related Programs:
- python different ways to iterate over a list in reverse order
- python program to print all permutations of a string in lexicographic order without recursion
- how to iterate a map in reverse order cpp
- python sort a list of numbers in ascending or descending order list sort vs sorted
- cpp how to reverse a list or sub list in place
- how to reverse a 1d 2d numpy array using np flip and operator in python
- how to copy all values from a map to a vector in cpp