Search code examples
c++vectorstlstd

How do I reverse a C++ vector?


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

Or do you just have to do it manually?


Solution

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