How to Reverse a Vector Using a Stack in C++? Last Updated : 09 Feb, 2024 Comments Improve Suggest changes Like Article Like Report In C++, the stack container follows the LIFO (Last In First Out) order of operation. It means that the element first inserted will come out at last while the element that was last inserted will come out first. In this article, we will learn how we can use stack to reverse a vector in C++ Example Input: myVector = {1,2,3,4,5}Output: Reversed Vector: 5 4 3 2 1Reverse a Vector Using Stack in C++We can use the std::stack to reverse the elements of the vector using the following algorithm. First, create a stack and push all the elements in the stack one by one.Once all elements of a vector are pushed, start traversing the vector and replacing the vector element with the top of the stack. Then pop that element from the stack.Keep doing it till the stack is empty.C++ Program to Reverse a Vector Using StackThe below program demonstrates how we can reverse a vector using a stack. C++ // C++ program to reverse a vector using stack #include <iostream> #include <stack> #include <vector> using namespace std; void reverseVectorUsingStack(vector<int>& vec) { // creating a auxilliary stack to store elements of // vector temporarily stack<int> auxStack; // Push elements onto the stack for (const auto& element : vec) { auxStack.push(element); } // Pop elements from the stack to reverse the vector for (auto& element : vec) { element = auxStack.top(); auxStack.pop(); } } int main() { vector<int> numbers = { 1, 2, 3, 4, 5 }; // Before reversal cout << "Original Vector: "; for (const auto& num : numbers) { cout << num << " "; } // Reverse the vector using the stack reverseVectorUsingStack(numbers); // After reversal cout << "\nReversed Vector: "; for (const auto& num : numbers) { cout << num << " "; } return 0; } OutputOriginal Vector: 1 2 3 4 5 Reversed Vector: 5 4 3 2 1 Time Complexity: O(N), where N is the number of elements in the vector.Auxiliary Space: O(N) Comment More infoAdvertise with us Next Article How to Reverse a Vector Using a Stack in C++? uk1124 Follow Improve Article Tags : C++ Programs C++ STL cpp-vector cpp-stack CPP Examples +2 More Practice Tags : CPPSTL Similar Reads How to Reverse a Vector using STL in C++? Reversing the vector means the swapping last element and first element, second last element and second element, and so on. In this article, we will learn how to reverse a vector using STL in C++.The most efficient method to reverse the vector is by using reverse() function. Letâs take a look at a si 3 min read How to Reverse a Word Using Stack in C++? In C++, we have a stack data structure that follows the Last In First Out (LIFO) principle. In this article, we will learn how to reverse a word using a stack in C++. Example: Input:word = "GeeksforGeeks"Output:Reversed Word: skeeGrofskeeGReverse a String Using Stack in C++To reverse a word using a 2 min read How to Reverse an Array using STL in C++? Reversing an array means rearranging its elements so that the first element becomes the last, the second element becomes the second last, and so on. In this article, we will learn how to reverse an array using STL in C++.The most efficient way to reverse an array using STL is by using reverse() func 2 min read How to Reverse a Stack in C++? In C++, stacks are containers that store the elements in the last in-first out order(LIFO). In, this article, we will learn how we can reverse a stack in C++. Example Input: stack<int> S ={5,4,3,2,1} store Output: // Reversed Stack stack<int> S ={1,2,3,4,5}Reverse a Stack in C++We can re 2 min read How to Reverse a String in C++? Reversing a string means replacing the first character with the last character, second character with the second last character and so on. In this article, we will learn how to reverse a string in C++.ExamplesInput: str = "Hello World"Output: dlroW olleHExplanation: The last character is replaced by 2 min read How to Reverse Iterate a Vector in C++? In this article, we will learn different methods to iterate through the vector in reverse order in C++.The most efficient method to iterate through the vector in reverse order is by using reverse iterator. Letâs take a look at an example:C++#include <bits/stdc++.h> using namespace std; int mai 2 min read How to Create a Stack of Vectors in C++? In C++, a stack of vectors can be created using the Standard Template Library (STL). The stack is a container adapter that provides a Last-In-First-Out (LIFO) type of data structure, and a vector is a dynamic array that can grow and shrink in size. In this article, we will learn how to create a stac 2 min read How to Traverse Vector Using const_reverse_iterator in C++? In C++, a vector can be traversed in reverse order using a const_reverse_iterator. A const_reverse_iterator is a type of iterator that points to the last element in the container and moves in the reverse direction. In this article, we will learn how to traverse a vector using a const_reverse_iterato 2 min read How to Use Iterator with a Vector in C++? In C++, vectors are the same as dynamic arrays with the ability to resize themselves automatically when an element is inserted or deleted. An iterator is a pointer-like object that can be used to access elements of a container (like an array or a vector). In this article, we will learn how to use an 2 min read How to Reverse a String in Place in C++? In C++, reversing a string is a basic operation in programming that is required in various applications, from simple and complex algorithms. Reversing a string in place involves changing the characters of the string directly without using input-dependent additional storage. In this article, we learn 2 min read Like