stack swap() in C++ STL Last Updated : 24 Jul, 2024 Comments Improve Suggest changes Like Article Like Report Stacks are a type of container adaptors with LIFO(Last In First Out) type of work, where a new element is added at one end and (top) an element is removed from that end only.stack::swap()This function is used to swap the contents of one stack with another stack of same type but the size may vary. Syntax :stackname1.swap(stackname2)Parameters: The name of the stack with which the contents have to be swapped. Result: All the elements of the 2 stack are swapped. Examples:contents of the stack from top to bottom areInput : mystack1 = {4, 3, 2, 1} mystack2 = {9, 7 ,5, 3} mystack1.swap(mystack2);Output : mystack1 = 9, 7, 5, 3 mystack2 = 4, 3, 2, 1Input : mystack1 = {7, 5, 3, 1} mystack2 = {8, 6, 4, 2} mystack1.swap(mystack2);Output : mystack1 = 8, 6, 4, 2 mystack2 = 7, 5, 3, 1Note: In stack container, the elements are printed in reverse order because the top is printed first then moving on to other elements. CPP // CPP program to illustrate // Implementation of swap() function #include <stack> #include <iostream> using namespace std; int main() { // stack container declaration stack<int> mystack1; stack<int> mystack2; // pushing elements into first stack mystack1.push(1); mystack1.push(2); mystack1.push(3); mystack1.push(4); // pushing elements into 2nd stack mystack2.push(3); mystack2.push(5); mystack2.push(7); mystack2.push(9); // using swap() function to swap elements of stacks mystack1.swap(mystack2); // printing the first stack cout<<"mystack1 = "; while (!mystack1.empty()) { cout<<mystack1.top()<<" "; mystack1.pop(); } // printing the second stack cout<<endl<<"mystack2 = "; while (!mystack2.empty()) { cout<<mystack2.top()<<" "; mystack2.pop(); } return 0; } Output:mystack1 = 9 7 5 3mystack2 = 4 3 2 1Time Complexity: O(1)Auxiliary Space: O(n) Comment More infoAdvertise with us Next Article List of Stacks in C++ STL A AyushSaxena Follow Improve Article Tags : Misc C++ STL cpp-stack cpp-stack-functions +1 More Practice Tags : CPPMiscSTL Similar Reads Stack in C++ STL In C++, stack container follows LIFO (Last In First Out) order of insertion and deletion. It means that most recently inserted element is removed first and the first inserted element will be removed last. This is done by inserting and deleting elements at only one end of the stack which is generally 5 min read stack empty() and stack size() in C++ STL The std::stack::size() and std::stack::empty() in C++ are built-in functions that are used to provide information about the size of the stack. They are the member functions of the std::stack container defined inside <stack> header file.stack::empty()The stack::empty() method is used to check w 2 min read stack::push() and stack::pop() in C++ STL The stack::push() and stack::pop() method in stack container is used to insert and delete the element from the top of stack. They are the member functions of std::stack container defined inside <stack> header file. In this article, we will learn how to use stack::push() and stack::pop() method 2 min read stack top() in C++ STL In C++, the std::stack::top() is used to find the top element of the std::stack container. It is a member function of std::stack class defined inside the <stack> header file. In this article, we will learn how to find the top element of stack using stack::top() in C++.Example:C++#include <b 2 min read stack emplace() in C++ STL Stacks are a type of container adaptors with LIFO(Last In First Out) type of working, where a new element is added at one end (top) and an element is removed from that end only. stack::emplace() This function is used to insert a new element into the stack container, the new element is added on top o 3 min read stack swap() in C++ STL Stacks are a type of container adaptors with LIFO(Last In First Out) type of work, where a new element is added at one end and (top) an element is removed from that end only.stack::swap()This function is used to swap the contents of one stack with another stack of same type but the size may vary. Sy 2 min read List of Stacks in C++ STL Prerequisite: List, Stack Lists are sequence containers that allow non-contiguous memory allocation. As compared to vector, list has slow traversal, but once a position has been found, insertion and deletion are quick. Syntax: list <Type> name_of_list; Stack are a type of container adaptor wit 3 min read Implementing Stack Using Class Templates in C++ The task is to implement some important functions of stack like pop(), push(), display(), topElement(), isEmpty(), isFull() using class template in C++. Stack is a linear data structure that follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or 5 min read How to implement a Stack using list in C++ STL In this article, we will discuss how to implement a Stack using list in C++ STL. Stack is a linear data structure which follows. LIFO(Last In First Out) or FILO(First In Last Out). It mainly supports 4 major operations:1. Push: Push an element into the stack.2. Pop: Removes the element by following 3 min read Queue using Stacks Given a stack that supports push and pop operations, your task is to implement a queue using one or more instances of that stack along with its operations.Table of ContentBy Making Enqueue Operation CostlyBy Making Dequeue Operation Costly Queue Implementation Using One Stack and RecursionBy Making 11 min read Like