Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • 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
  • C++ Data Types
  • C++ Input/Output
  • C++ Arrays
  • C++ Pointers
  • C++ OOPs
  • C++ STL
  • C++ Interview Questions
  • C++ Programs
  • C++ Cheatsheet
  • C++ MCQ
  • C++ Projects
  • C++ Exception Handling
  • C++ Memory Management
Open In App
Next Article:
Map of Sets in C++ STL with Examples
Next article icon

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

Output
1 => For Geeks  2 => Is The  3 => Great Learning  4 => Platform 

Next Article
Map of Sets in C++ STL with Examples

B

bhuwanesh
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
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