How to Store Multiple Values for the Same Key in C++? Last Updated : 19 Mar, 2024 Comments Improve Suggest changes Like Article Like Report In C++ STL, maps are used to store key-value pairs but they only allow one value for a key. Sometimes, we might want multiple values to be associated with a single key. In this article, we will learn how to store multiple values for the same key in C++. Example: Input:Key:1; Value:RedKey:1; Value:BlueKey:1; Value:GreenKey:2; Value:AppleKey:2; Value:MangoKey:2; Value:BananaOutput:Key: 1, Values: Red Blue Green Key: 2, Values: Apple Mango BananaMultiple Values for the Same Key in C++In C++, if we want to associate multiple values with the same key, we can use std::multimap that allows us to store multiple elements that have the same key and also permits the storage of duplicate keys for the same value. To insert elements into the multimap, use the std::multimap::insert() function. C++ Program to Use Multiple Values for the Same Key The below program demonstrates how we can use multiple values for the same key in C++. C++ // C++ Program to illustrate how to use multiple values for // the same key #include <iostream> #include <map> using namespace std; int main() { // Declaration of the multimap multimap<int, string> mm; // Inserting keys and their respective values into the // multimap mm.insert({ 1, "Red" }); mm.insert({ 1, "Blue" }); mm.insert({ 1, "Green" }); mm.insert({ 2, "Apple" }); mm.insert({ 2, "Mango" }); mm.insert({ 2, "Banana" }); // Printing the keys and their respective values cout << "Multimap: " << endl; auto it = mm.begin(); while (it != mm.end()) { int key = it->first; cout << "Key: " << key << ", Values: "; while (it != mm.end() && it->first == key) { cout << it->second << " "; it++; } cout << endl; } return 0; } OutputMultimap: Key: 1, Values: Red Blue Green Key: 2, Values: Apple Mango Banana Time Complexity: O(log n), here n is the number of elements in the multimap.Auxilliary Space : O(n) Comment More infoAdvertise with us Next Article How to Store Multiple Values for the Same Key in C++? A abhay94517 Follow Improve Article Tags : C++ Programs C++ cpp-multimap CPP Examples Practice Tags : CPP Similar Reads How to Add Multiple Key-Value Pairs to a Map in C++? In C++, a map is a container that stores elements in key-value pairs, where each key is unique and associated with a value. In this article, we will learn how to add multiple key-value pairs to a map in C++. Example: Input: myMap = { {1, "one"} }; Elements to be insert = { {2, "two"}, {3, "three"}} 2 min read How To Delete Multiple Key-Value Pairs From Multimap in C++? In C++, a multimap is a container that stores elements where each element has a key value and a mapped value. Unlike maps, multimaps allow multiple key-value pairs with the same key. In this article, we will learn how to delete multiple key-value pairs from a multimap in C++. Example Input: myMultim 2 min read How to Store Vectors as Keys in a Multimap in C++? In C++, the std::multimap is a container that stores elements in a key-value pair, whereas std::vector is a sequence container that stores elements in contiguous memory. In this article, we will learn how to store vectors as keys in a multimap in C++. Example: Input:myVector ={1,2,3};myVector ={4,5, 2 min read How to Take Multiple Input from User in C++? In C++, we use cin when we want to take input from the user. We often also need to take more than one input at a time. In this article, we will learn how to take multiple inputs in C++. Take Multiple Inputs from a User in C++To take multiple inputs from users, we can repeatedly use the std::cin usin 2 min read How to Update Value of a Key in Map in C++? In C++, a map is a data structure that stores key-value pairs. In this article, we will learn how to change the value of a key that is already present in the map container in C++. Example Input: map<int, string> mp = {(1, "One"), (2,"Two")} Output: map after Updation: {(1, "One"), (2, "Three") 2 min read Like