How to Create a Stack of Pairs in C++? Last Updated : 06 Mar, 2024 Comments Improve Suggest changes Like Article Like Report In C++, Stacks are a type of container adaptor 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. A pair is a simple container that stores data in a key and value format. In this article, we will learn how to create a stack of pairs in C++. Example Input: myPair1 = {1, “C++”}; myPair2 = {2, “Java”}; Output: myStack = [ {1, “C++”}, {2, “Java”} ]Stack of Pairs in C++To create a stack of pairs in C++, we will pass the template parameter in the stack declaration as a pair. This will create a stack where each element is a pair. Syntax to Declare Stack of Multisetstack <pair <keyType, valueType>> myStack;C++ Program to Create a Stack of PairsThe below example demonstrates how we can create a stack of pairs in C++. C++ // C++ Program to illustrate how to create a stack of pairs #include <iostream> #include <stack> #include <utility> using namespace std; int main() { // Initialize two pairs pair<int, string> myPair1 = make_pair(1, "C++"); pair<int, string> myPair2 = make_pair(2, "Java"); // Create a stack of pairs stack<pair<int, string> > myStack; myStack.push(myPair1); myStack.push(myPair2); // Print the stack of pairs while (!myStack.empty()) { pair<int, string> topPair = myStack.top(); myStack.pop(); cout << "{" << topPair.first << ", " << topPair.second << "}, "; } cout << endl; return 0; } Output{2, Java}, {1, C++}, Time Complexity: O(N), where N is the number of pairs.Auxiliary Space: O(N) Comment More infoAdvertise with us Next Article How to Create a Stack of Pairs in C++? A abhaystriver Follow Improve Article Tags : C++ Programs C++ STL cpp-pair cpp-stack CPP Examples +2 More Practice Tags : CPPSTL Similar Reads How to Create a Set of Pairs in C++? In C++, sets are associative containers that store unique elements. On the other hand, pairs allow the users to store two data of different or the same type into a single object. In this article, we will learn how we can create a set of pairs in C++. Example Input: p1={1, 2} p2 ={3, 4} Output: Eleme 2 min read How to Create a Stack of Set in C++? In C++ STL, Stacks are a type of container adaptor 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. Sets are a type of associative container in which each element is unique and in some sorted order. In this arti 2 min read How to Create a Stack of Stack in C++? In C++, the stack is a container that follows the LIFO (Last In, First Out) order in which the elements are inserted and removed from it. In this article, we will learn how to create a stack of a stack in C++. Example:Input:Elements in stack1= 1, 2, 3, 4Elements in stack2= 5, 6, 7Output:Elements in 2 min read How to Create a Vector of Pairs in C++? In C++, std::pair is the data type that stores the data as keys and values. On the other hand, std::vector is an STL container that stores the collection of data of similar type in the contiguous memory location. In this article, we will learn how to combine these two to create a vector of pairs in 5 min read How to Create a Stack of Queue in C++? In C++, the std::stack is a container that follows the LIFO (Last In, First Out) rule, whereas std::queue is a container that follows the FIFO (First In, First Out) rule. In this article, we will learn how to create a stack of a queue in C++. Example: Input:myQueue = { a, b, c };myQueue = { d, e };O 2 min read Like