Map of Sets in C++ STL with Examples Last Updated : 18 Feb, 2023 Comments Improve Suggest changes Like Article Like Report Maps are associative containers that store elements in a mapped fashion. Each element has a key value and a mapped value. No two mapped values can have same key values. Sets are a type of associative container in which each element has to be unique because the value of the element identifies it. The value of the element cannot be modified once it is added to the set, though it is possible to remove and add the modified value of that element. Map of Sets in STL: A map of sets can be very useful in designing complex data structures and algorithms. Syntax: map<datatype, set<datatype>> map_of_set : stores a set of datatypes corresponding to a datatype or map<set<datatype>, datatype> map_of_set : stores a datatype corresponding to a set of datatypes Let's see how to implement Maps of Sets in C++: C++ // C++ program to demonstrate use of map of set #include <bits/stdc++.h> using namespace std; void show(map<int, set<string> >& mapOfSet) { // Using iterator to access // key, value pairs present // inside the mapOfSet for (auto it = mapOfSet.begin(); it != mapOfSet.end(); it++) { // Key is integer cout << it->first << " => "; // Value is a set of string set<string> st = it->second; // Strings will be printed // in sorted order as set // maintains the order for (auto it = st.begin(); it != st.end(); it++) { cout << (*it) << ' '; } cout << '\n'; } } // Driver code int main() { // Declaring a map whose key // is of integer type and // value is a set of string map<int, set<string> > mapOfSet; // Inserting values in the // set mapped with key 1 mapOfSet[1].insert("Geeks"); mapOfSet[1].insert("For"); // Set stores unique or // distinct elements only mapOfSet[1].insert("Geeks"); // Inserting values in the // set mapped with key 2 mapOfSet[2].insert("Is"); mapOfSet[2].insert("The"); // Inserting values in the // set mapped with key 3 mapOfSet[3].insert("Great"); mapOfSet[3].insert("Learning"); // Inserting values in the // set mapped with key 4 mapOfSet[4].insert("Platform"); show(mapOfSet); return 0; } Output1 => For Geeks 2 => Is The 3 => Great Learning 4 => Platform Comment More infoAdvertise with us Next Article Map of Sets in C++ STL with Examples B bhuwanesh Follow Improve Article Tags : C++ cpp-map cpp-set Practice Tags : CPP Similar Reads Map of Tuples in C++ with Examples What is a tuple? A tuple in C++ is an object that has the ability to group a number of elements. The elements can be of the same type as well as different data types. The order in which tuple elements are initialized can be accessed in the same order. Functions associated with a tuple: 1. make_tuple 4 min read Set of Tuples in C++ with Examples What is a tuple?A tuple is an object that can hold a number of elements. The elements can be of different data types. The elements of tuples are initialized as arguments in the order in which they will be accessed. Operations on tuple:1. get(): get() is used to access the tuple values and modify the 4 min read Map of Vectors in C++ STL with Examples Map in STL Maps are associative containers that store elements in a mapped fashion. Each element has a key value and a mapped value. No two mapped values can have same key values. Vector in STL Vector is same as dynamic arrays with the ability to resize itself automatically when an element is insert 2 min read Multimap vs Map in C++ STL with Examples Map in C++ STL Map stores unique key-value pairs in a sorted manner. Each key is uniquely associated with a value that may or may not be unique. A key can be inserted or deleted from a map but cannot be modified. Values assigned to keys can be changed. It is a great way for quickly accessing value u 8 min read Vector of Unordered Maps in C++ with Examples Vector: Vector: It is the same as dynamic arrays with the ability to resize itself automatically when an element is inserted or deleted, with their storage being handled automatically by the container. Vector elements are placed in contiguous storage so that they can be accessed and traversed using 6 min read Like