Skip to content
geeksforgeeks
  • 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
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • C++
  • Standard Template Library
  • STL Vector
  • STL List
  • STL Set
  • STL Map
  • STL Stack
  • STL Queue
  • STL Priority Queue
  • STL Interview Questions
  • STL Cheatsheet
  • C++ Templates
  • C++ Functors
  • C++ Iterators
Open In App
Next Article:
How to Create a Stack of Multimap in C++?
Next article icon

How to Create a Multimap of Vectors in C++?

Last Updated : 27 Feb, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In C++, a multimap is similar to a map with the addition that multiple elements can have the same keys. Also, it is not required that the key-value and mapped value pair have to be unique in this case. In this article, we will learn how to create a multimap of vectors in C++.

For Example,

Input:
myPair1 = {1, {2, 3, 4}}
myPair2 = {2, {5, 6, 7}}Output:
MultiMap = { {1, {2, 3, 4}}, {2, {5, 6, 7}} }

Creating a Multimap of Vectors in C++

To create a multimap of vectors, we will have to first declare a multimap where in each key-value pair, the key is of type string and the value is a vector.

We can then use the std::multimap::insert() function to insert key-value pairs into the multimap. The key is a string, and the value is a vector.

Syntax to Create a Multimap of Vectors

 multimap<string, vector<int> > myMultimap;

C++ Program to Create a Multimap of Vectors

C++
// C++ program to create a multimap of vectors  #include <iostream> #include <map> #include <vector> using namespace std;  int main() {     // Creating a multimap of string and vector     multimap<string, vector<int> > myMultimap;      // Inserting key-value pairs into the multimap     myMultimap.insert({ "apple", { 1, 2 } });     myMultimap.insert({ "banana", { 2, 3, 4 } });     myMultimap.insert({ "cherry", { 3, 4, 5, 6 } });     myMultimap.insert({ "apple", { 1, 2 } });      // Displaying the multimap elements     for (auto it = myMultimap.begin();          it != myMultimap.end(); ++it) {         cout << it->first << " ";         for (auto vec_it = it->second.begin();              vec_it != it->second.end(); ++vec_it) {             cout << *vec_it << " ";         }         cout << endl;     }      return 0; } 

Output
apple 1 2   apple 1 2   banana 2 3 4   cherry 3 4 5 6     

Time Complexity: O(N * log N), where N is the number of arrays.
Space Complexity: O(N * M), where M is the average size of the arrays.




Next Article
How to Create a Stack of Multimap in C++?

R

rohitpant4532
Improve
Article Tags :
  • C++ Programs
  • C++
  • STL
  • cpp-vector
  • cpp-multimap
  • CPP Examples
Practice Tags :
  • CPP
  • STL

Similar Reads

  • How to Create a Stack of Multimap 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 multimap is a container that stores key-value pairs in an ordered manner. In this article, we will learn how to c
    2 min read
  • How to Create a Set of Vectors in C++?
    In C++, a set is a type of associative container in which duplicate elements are not allowed and a vector is a dynamic array in which duplicate elements are allowed. In this article, we will learn how to create a set of vectors in C++. For Example, Input:vector<int> vec1={1, 2, 3};vector<in
    2 min read
  • How to Create a Multimap of Arrays in C++?
    In C++, a multimap is similar to a map with the addition that multiple elements can have the same keys. Also, it is NOT required that the key-value and mapped value pair have to be unique in this case. In this article, we will learn how to create a multimap of arrays in C++ STL. Example Input: myArr
    2 min read
  • How to Create a Unordered Multimap of Tuples in C++?
    In C++, an unordered multimap container stores key-value pairs in no particular order. Unlike a map, an unordered multimap allows multiple values to be associated with a single key. In this article, we will learn how to create an unordered multimap of tuples in C++. For Example, Input: myPair1 = { "
    2 min read
  • How to Create a Stack of Vectors in C++?
    In C++, a stack of vectors can be created using the Standard Template Library (STL). The stack is a container adapter that provides a Last-In-First-Out (LIFO) type of data structure, and a vector is a dynamic array that can grow and shrink in size. In this article, we will learn how to create a stac
    2 min read
  • How to Create a Deque of Vectors in C++?
    In C++, deques are sequence containers similar to queues but unlike queues, deques allow the insertion and deletion of elements from both ends efficiently. Vectors are dynamic arrays that can resize themselves during the runtime. In this article, we will learn how to create a deque of vectors in C++
    2 min read
  • How to Create a Stack of Unordered_Multimap in C++?
    In C++, an unordered_multimap is an associative container that contains key-value pairs allowing multiple elements with the same key. In this article, we will learn how to create a stack of unordered_multimaps in C++. Example: Input: myMultimap1 = { {1, “C++”}, {2, “Java”}, {1, “Python”} }; myMultim
    2 min read
  • How to Create Deque of Multimap in C++?
    In C++, a deque (double-ended queue) is a data structure that allows insertion and deletion at both ends, while a multimap is an associative container that contains key-value pairs, where multiple keys can have the same value. In this article, we will learn how to create a deque of multimaps in C++
    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 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
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