Reverse a vector in C++
This post will discuss how to reverse a vector in C++.
1. Using std::reverse function
The simplest solution is to use the std::reverse function defined in the <algorithm> header. This function internally uses std::iter_swap for swapping the elements from both ends of the given range.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
#include <iostream> #include <vector> #include <algorithm> template <typename T> std::ostream &operator<<(std::ostream &os, const std::vector<T> &v) { for (T const &i: v) { os << i << " "; } return os; } int main() { std::vector<int> v = { 1, 2, 3, 4, 5 }; std::cout << "Original Vector: " << v << std::endl; std::reverse(v.begin(), v.end()); std::cout << "Reversed Vector: " << v << std::endl; return 0; } |
Output:
Original Vector: 1 2 3 4 5
Reversed Vector: 5 4 3 2 1
2. Using Reverse Iterators
Here, the idea is to use reverse iterators to construct a new vector using its range constructor. Then we can simply swap the original vector with the new vector.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
#include <iostream> #include <vector> #include <algorithm> template <typename T> std::ostream& operator<<(std::ostream& os, const std::vector<T> &v) { for (T const& i: v) { os << i << " "; } return os; } int main() { std::vector<int> v = { 1, 2, 3, 4, 5 }; std::cout << "Original Vector: " << v << std::endl; std::vector<int> r(v.rbegin(), v.rend()); v.swap(r); // or, call `v = r` std::cout << "Reversed Vector: " << v << std::endl; return 0; } |
3. Using std::swap function
We have seen that std::reverse function internally uses std::iter_swap function. The same can be achieved with the help of the std::swap function, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
#include <iostream> #include <vector> #include <algorithm> template <typename T> std::ostream& operator<<(std::ostream& os, const std::vector<T> &v) { for (T const& i: v) { os << i << " "; } return os; } int main() { std::vector<int> v = { 1, 2, 3, 4, 5 }; std::cout << "Original Vector: " << v << std::endl; for (auto start = v.begin(), end = std::prev(v.end()); start < end; ++start, --end) { std::swap(*start, *end); } std::cout << "Reversed Vector: " << v << std::endl; return 0; } |
4. Using std::transform function
Finally, another good solution is to use the std::transform algorithm, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
#include <iostream> #include <vector> #include <algorithm> template <typename T> std::ostream& operator<<(std::ostream& os, const std::vector<T> &v) { for (T const& i: v) { os << i << " "; } return os; } int main() { std::vector<int> v = { 1, 2, 3, 4, 5 }; std::cout << "Original Vector: " << v << std::endl; auto mid = v.begin() + v.size()/2; std::transform(v.begin(), mid, v.rbegin(), v.begin(), [](int &x, int &y) { std::swap(x, y); return x; }); std::cout << "Reversed Vector: " << v << std::endl; return 0; } |
That’s all about reversing a vector in C++.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)