How to Create a Stack of Set in C++? Last Updated : 05 Mar, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 article, we will learn how to create a stack of sets in C++. Example Input:mySet1 = {1, 4, 8, 9, 11}mySet2 = {1, 2, 3, 5, 7}Output:myStack: [ {1, 4, 8, 9, 11} {1, 2, 3, 5, 7} ]Creating a Stack of Sets in C++To create a std::stack of std::set in C++, we can pass the type of the stack container to be of type std::set as a template parameter while declaration. Syntax to Declare Stack of Setstack<set<type>> myStackC++ Program to Create a Stack of Set C++ // C++ Program to illustrate how to create a stack of set #include <iostream> #include <set> #include <stack> using namespace std; int main() { // Declaring a stack of sets stack<set<int> > myStack; // Creating some sets set<int> set1 = { 1, 2, 3 }; set<int> set2 = { 4, 5, 6 }; set<int> set3 = { 7, 8, 9 }; // Pushing sets into the stack myStack.push(set1); myStack.push(set2); myStack.push(set3); // Checking if the stack is empty cout << "myStack: " << endl; int i = 1; while (!myStack.empty()) { auto ele = myStack.top(); cout << "Set" << i++ << ": "; for (auto i : ele) { cout << i << " "; } cout << endl; myStack.pop(); } return 0; } OutputmyStack: Set1: 7 8 9 Set2: 4 5 6 Set3: 1 2 3 Time complexity: O(N), where N is the number of sets.Auxiliary Space: O(N) Comment More infoAdvertise with us Next Article How to Create a Stack of Stack in C++? G gauravggeeksforgeeks Follow Improve Article Tags : C++ Programs C++ STL cpp-set cpp-stack CPP Examples +2 More Practice Tags : CPPSTL Similar Reads 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 Set of Sets in C++? In C++, sets are STL containers that store unique elements of the same type in a sorted manner. Sets of sets, also known as nested sets, are collections in which each element of the outer set contains another set as its element. In this article, we will learn how to create a set of sets in C++. Set 2 min read How to Create a Stack of Pairs in C++? 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 crea 2 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 How to Create a Stack of Map in C++? In C++, the std::stack is a container that follows the LIFO (Last In, First Out) rule, whereas std::map is an associative container that stores key-value pairs. In this article, we will learn how to create a stack of a map in C++. Example:Input: myMap = { {1: âaâ}, {2: âbâ}, {3: âcâ} }; myMap = { {4 2 min read How to Create a Stack of Lists in C++? In C++, a list is a sequence container that allows dynamic insertion and deletion operations, whereas a stack is a data structure that follows last-in, first-out (LIFO). In this article, we will learn how to create a stack of lists in C++. Example: Input: list1 = { 1, 2, 3, 4 }list2 = { 5, 6, 7 }Out 2 min read Like