How do I reverse a C++ vector?

SirYakalot picture SirYakalot · Jan 16, 2012 · Viewed 143k times · Source

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

Or do you just have to do it manually?

Answer

Ivaylo Strandjev picture Ivaylo Strandjev · Jan 16, 2012

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;
}