Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • C++
  • Standard Template Library
  • STL Vector
  • STL List
  • STL Set
  • STL Map
  • STL Stack
  • STL Queue
  • STL Priority Queue
  • STL Interview Questions
  • STL Cheatsheet
  • C++ Templates
  • C++ Functors
  • C++ Iterators
Open In App
Next Article:
How to Reverse a Word Using Stack in C++?
Next article icon

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 1

Reverse 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 Stack

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

Output
Original 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)




Next Article
How to Reverse a Word Using Stack in C++?
author
uk1124
Improve
Article Tags :
  • C++ Programs
  • C++
  • STL
  • cpp-vector
  • cpp-stack
  • CPP Examples
Practice Tags :
  • CPP
  • STL

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 s
    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() fun
    3 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++. Examples Input: str = "Hello World"Output: dlroW olleHExplanation: The last character is replaced
    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: [GFGTABS] C++ #include <bits/stdc++.h> using namespace
    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 Insert into Vector Using Iterator in C++?
    In C++, a vector is a dynamic array that can grow and shrink in size as needed. Vectors are sequence containers representing arrays that can change in size. In this article, we will learn how to insert elements into a vector using an iterator in C++. Example: Input: myVector = {10,20,30}; Output: Ve
    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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences