It's because you're swapping twice for each element.

For a vector of size of 4: Swap operations:

0 3
1 2
2 1
3 0

Loop over the half size of the vector.

for (int i = 0, j = names.size() - 1; i < names.size()/2; i++, j--)
    {
        std::string temp = names[i];
        names[i] = names[j];
        names[j] = temp;
    }

Print the vector using another loop.

for (int i = 0; i < names.size(); i++)
    {
        cout<<names[i]<<endl;
    }
Answer from Kamol Hasan on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › c++ › how-to-reverse-a-vector-using-stl-in-c
How to Reverse a Vector using STL in C++? - GeeksforGeeks
July 11, 2025 - The most efficient method to reverse the vector is by using reverse() function.
Discussions

How do I reverse a C++ vector? - Stack Overflow
Is there a built-in vector function in C++ to reverse a vector in place? Or do you just have to do it manually? More on stackoverflow.com
🌐 stackoverflow.com
string reverse help
You’re assigning a string to a vector. You’d want to push_back(instr) or use an initialiser list. You don’t even need the vector tbh you can use a reverse iterator on the string to print it out Edit: outstr is a string and you’re redclaring it to a vector which is the error More on reddit.com
🌐 r/Cplusplus
15
0
September 21, 2025
Vectors-- reversing an array - C++ Forum
Hello, I am prompted to write a program that take an array of user inputted integers and then prints them in reverse order. I have written the code below thus far, which is a combination of two set of code I had written earlier that worked just, although I do understand their connection may ... More on cplusplus.com
🌐 cplusplus.com
Reverse an array without a temp variable?
NO ARRAY EVER NEEDS TO BE REVERSED. More on reddit.com
🌐 r/programming
85
2
February 22, 2010
🌐
Quora
quora.com › How-can-you-reverse-an-array-in-C-without-using-third-party-libraries
How to reverse an array in C++ without using third party libraries - Quora
Answer: Two scenarios: * In place * Copy Copy is simplest and C/C++ would produce best possible code, will use two pointers with decrement and increment. For example ARM has STMED and LDMED instructions which store in memory a number of registers. Compiler I used recognized them and used them...
🌐
Edureka Community
edureka.co › home › community › categories › c++ › how do i reverse a c vector
How do I reverse a C vector | Edureka Community
June 27, 2022 - How do I reverse a C vector · How to reverse a number in C++? Oct 28, 2024 · undefined reference to `get_string' cs50 library Sep 16, 2023 · fatal error: cs50.h (c language library) : No such file or directory Sep 16, 2023 · Suggestions of excellent examples of real C/C++ code Dec 17, 2022 ·
🌐
Cppreference
en.cppreference.com › w › cpp › algorithm › reverse.html
std::reverse - cppreference.com
February 9, 2025 - Implementations (e.g. MSVC STL) may enable vectorization when the iterator type satisfies LegacyContiguousIterator and swapping its value type calls neither non-trivial special member function nor ADL-found swap. ... #include <algorithm> #include <iostream> #include <iterator> #include <vector> void println(auto rem, auto const& v) { for (std::cout << rem; auto e : v) std::cout << e << ' '; std::cout << '\n'; } int main() { std::vector<int> v {1, 2, 3}; std::reverse(v.begin(), v.end()); println("after reverse, v = ", v); int a[] = {4, 5, 6, 7}; std::reverse(std::begin(a), std::end(a)); println("after reverse, a = ", a); }
🌐
Cprogramming
cboard.cprogramming.com › cplusplus-programming › 115989-reverse-member-function.html
reverse member function
May 17, 2009 - It occurred to me that perhaps you misread reserve as reverse. The former is the name of a member function of std::vector. ... Will that work if I try the <algorithm> header? No, unless your standard library implementation provided std::vector with a reverse member function as a library extension.
Find elsewhere
🌐
TutorialsPoint
tutorialspoint.com › how-to-reverse-a-vector-using-stl-in-cplusplus
How to reverse a Vector using STL in C++?
January 30, 2025 - #include <iostream> #include<vector> using namespace std; int main(){ vector<int> tutorial{11,22,33,44}; vector<int>::reverse_iterator x; for(x=tutorial.rbegin();x!=tutorial.rend();x++) std::cout<< *x<<" "; return 0; }
🌐
Educative
educative.io › answers › what-is-the-stdreverse-function-in-cpp
What is the std::reverse() function in C++?
std::reverse() is a built-in function in C++'s Standard Template Library. The function takes in a beginning iterator, an ending iterator, and reverses the order of the element in the given range · Take a look at the function signature of ...
🌐
Reddit
reddit.com › r/cplusplus › string reverse help
r/Cplusplus on Reddit: string reverse help
September 21, 2025 -

void restring()// ask user for string, then reverse it and output

{

std::string instr, outstr;

cout << "give me your favorite word: ";

std::getline(std::cin, instr);

std::vector<std::string>outstr = instr;

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

cout << endl << "your word forwards: " << instr << endl;

cout << "your word backwards: " << outstr << endl;

}

This is one of a few functions in a code. I'm trying to get the user to input a string so I can copy it, then reverse the copy, then output both strings. the line "std::vector<std::string>outstr = instr;" is the only one throwing an error in the code before I run it. I don't know why, please help. Thanks.

🌐
Cplusplus
cplusplus.com › reference › algorithm › reverse
std::reverse
template <class BidirectionalIterator> void reverse (BidirectionalIterator first, BidirectionalIterator last); ... Reverses the order of the elements in the range [first,last). The function calls iter_swap to swap the elements to their new locations.
🌐
GeeksforGeeks
geeksforgeeks.org › dsa › program-to-reverse-an-array
Array Reverse - GeeksforGeeks
Now, copy all elements from original array to the temporary array in reverse order. Finally, copy all the elements from temporary array back to the original array. ... #include <iostream> #include <vector> using namespace std; void reverseArray(vector<int> &arr) { int n = arr.size(); // Temporary array to store elements // in reversed order vector<int> temp(n); // Copy elements from original array // to temp in reverse order for(int i = 0; i < n; i++) temp[i] = arr[n - i - 1]; // Copy elements back to original array for(int i = 0; i < n; i++) arr[i] = temp[i]; } int main() { vector<int> arr = { 1, 4, 3, 2, 6, 5 }; reverseArray(arr); for(int i = 0; i < arr.size(); i++) cout << arr[i] << " "; return 0; }
Published   2 days ago
🌐
Scaler
scaler.com › home › topics › how to reverse a vector in c++?
How to Reverse a Vector in C++? - Scaler Topics
September 27, 2023 - This allows straightforward element ... manipulation. ... The std::transform() function, another utility from the <algorithm> library, can be used to reverse a vector's elements by applying a transformation operation....
🌐
GeeksforGeeks
geeksforgeeks.org › c++ › stdreverse-in-c
reverse() in C++ STL - GeeksforGeeks
#include <bits/stdc++.h> using namespace std; int main() { vector<int> v = {1, 2, 3, 4, 5}; // Reversing the vector reverse(v.begin(), v.end()); for (int i : v) cout << i << " "; return 0; } ... The reverse() function is defined in the <algorithm> ...
Published   January 20, 2026
🌐
DEV Community
dev.to › emilossola › reversing-a-vector-in-c-efficient-techniques-and-best-practices-436o
Reversing a Vector in C++: Efficient Techniques and Best Practices - DEV Community
July 17, 2023 - The in-place reverse technique also has a time complexity of O(n/2), which is more efficient than using a temporary vector. Using the std::reverse() Algorithm: C++ provides the std::reverse() algorithm in the library, which can be used to reverse ...
🌐
Cplusplus
cplusplus.com › forum › beginner › 232236
Vectors-- reversing an array - C++ Forum
The final code is as follows: #include <iostream> #include <vector> using namespace std; int main() { const int INTEGERS = 10; vector<int> userArray(INTEGERS); int i; cout << "Enter " << INTEGERS << " integers: "; for (int i = 0; i < INTEGERS; i++) { cin >> userArray[i]; } for (int i = INTEGERS - 1; i >= 0; i--) { cout << userArray[i] << " "; } //system("pause"); return 0; }
🌐
Reddit
reddit.com › r/programming › reverse an array without a temp variable?
r/programming on Reddit: Reverse an array without a temp variable?
February 22, 2010 -

Hey r/prog,

This is more of a trivia thing, I remember seeing how to reverse/sort an array with out using a temp var awhile back and saying "ohhh, why didn't I think of that before?" Well... now I'm back to that, except I can't find an example on how to it. I remember it being relatively simple, but I can't remember how to it.

Anyone care to enlighten me and anyone else who might fine themselves asking the same question? I don't care what language you do it in... although it might require pointer manipulation, I don't remember!

🌐
TutorialsPoint
tutorialspoint.com › write-a-c-program-to-reverse-a-string-without-using-a-library-function
Write a C program to Reverse a string without using a ...
September 10, 2023 - #include <stdio.h> int main() { char s1[] = "TajMahal"; // String Given char s2[8]; // Variable to store reverse string int length = 0; int loop = 0; while(s1[length] != '\0') { length++; } printf("\nPrinting in reverse - "); for(loop = --length; loop>=0; loop--) printf("%c", s1[loop]); loop = 0; printf("\nStoring in reverse - "); while(length >= 0) { s2[length] = s1[loop]; length--; loop++; } s1[loop] = '\0'; // Terminates the string printf("%s\n", s2); return 0; }