How do I reverse a C++ vector?

C++VectorStlStd

C++ Problem Overview


Is there a built-in vector function in C++ to reverse a vector in place?

Or do you just have to do it manually?

C++ Solutions


Solution 1 - C++

There's a function std::reverse in the algorithm header for this purpose.

#include <vector>
#include <algorithm>

int main() {
  std::vector<int> a;
  std::reverse(a.begin(), a.end());
  return 0;
}

Solution 2 - C++

All containers offer a reversed view of their content with rbegin() and rend(). These two functions return so-calles reverse iterators, which can be used like normal ones, but it will look like the container is actually reversed.

#include <vector>
#include <iostream>

template<class InIt>
void print_range(InIt first, InIt last, char const* delim = "\n"){
  --last;
  for(; first != last; ++first){
    std::cout << *first << delim;
  }
  std::cout << *first;
}

int main(){
  int a[] = { 1, 2, 3, 4, 5 };
  std::vector<int> v(a, a+5);
  print_range(v.begin(), v.end(), "->");
  std::cout << "\n=============\n";
  print_range(v.rbegin(), v.rend(), "<-");
}

Live example on Ideone. Output:

1->2->3->4->5
=============
5<-4<-3<-2<-1

Solution 3 - C++

You can use std::reverse like this

std::reverse(str.begin(), str.end());

Solution 4 - C++

Often the reason you want to reverse the vector is because you fill it by pushing all the items on at the end but were actually receiving them in reverse order. In that case you can reverse the container as you go by using a deque instead and pushing them directly on the front. (Or you could insert the items at the front with vector::insert() instead, but that would be slow when there are lots of items because it has to shuffle all the other items along for every insertion.) So as opposed to:

std::vector<int> foo;
int nextItem;
while (getNext(nextItem)) {
    foo.push_back(nextItem);
}
std::reverse(foo.begin(), foo.end());

You can instead do:

std::deque<int> foo;
int nextItem;
while (getNext(nextItem)) {
    foo.push_front(nextItem);
}
// No reverse needed - already in correct order

Solution 5 - C++

You can also use std::list instead of std::vector. list has a built-in function list::reverse for reversing elements.

Solution 6 - C++

#include<algorithm>
#include<vector>
#include<iostream>
using namespace std;
int main()
{
    vector<int>v1;
    for(int i=0; i<5; i++)
        v1.push_back(i*2);
    for(int i=0; i<v1.size(); i++)
        cout<<v1[i];    //02468
    reverse(v1.begin(),v1.end());
    
    for(int i=0; i<v1.size(); i++)
        cout<<v1[i];   //86420
}

Solution 7 - C++

You can also use reverse_iterator to create a new reverse vector.

#include <iostream>
#include <vector>

int main() {
    std::vector<int> v;
    std::vector<int> reverse_v(v.rbegin(), v.rend());

    return 0;
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDollarsliceView Question on Stackoverflow
Solution 1 - C++Ivaylo StrandjevView Answer on Stackoverflow
Solution 2 - C++XeoView Answer on Stackoverflow
Solution 3 - C++Chuck NorrisView Answer on Stackoverflow
Solution 4 - C++Arthur TaccaView Answer on Stackoverflow
Solution 5 - C++Hello WView Answer on Stackoverflow
Solution 6 - C++basharView Answer on Stackoverflow
Solution 7 - C++tk kimView Answer on Stackoverflow