How to Insert into Multimap using make_pair in C++? Last Updated : 06 Mar, 2024 Comments Improve Suggest changes Like Article Like Report In C++, a multimap is a container that stores key-value pairs in an ordered manner. Unlike a map, a multimap allows multiple values to be associated with a single key. In this article, we will learn how to insert into a multimap using make_pair in C++. Example Input: myMultimap = {{1, “C++”}, {2, “Java”}}; Pair to Insert: {1, “Python”}; Output: myMultimap = {1, C++}, {1, Python}, {2, Java}Insert into Multimap using make_pair in C++The std::make_pair() function constructs a pair of the given type and returns this pair. We can use this function to create a pair and directly insert it into the multimap container using std::multimap::insert(). Note that the pair type and multimap type should be the same. SyntaxmultiMap.insert(make_pair(1, "apple")); Where 1 is the key and "apple" is the associated value. C++ Program to Insert into Multimap using make_pair C++ // C++ Program to illustrate how to insert into a multimap // using make_pair #include <iostream> #include <map> using namespace std; int main() { // Initialize a multimap with some entries multimap<int, string> myMultimap = { { 1, "C++" }, { 2, "Java" } }; // Insert a new pair into the multimap using make_pair myMultimap.insert(make_pair(1, "Python")); // Print the multimap after insertion for (auto& pair : myMultimap) { cout << "{" << pair.first << ", " << pair.second << "}, "; } cout << endl; return 0; } Output{1, C++}, {1, Python}, {2, Java}, Time Complexity: O(logN) where N is the number of elements in the multimap. Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article How to Insert into Multimap using make_pair in C++? A anushamahajan5 Follow Improve Article Tags : C++ Programs C++ STL cpp-pair cpp-multimap CPP Examples +2 More Practice Tags : CPPSTL Similar Reads How To Insert Multiple Key-Value Pairs Into a Multimap in C++? In C++, a multimap is similar to a map that stores the data in the key-value format and it also allows us to store duplicate keys for the same value. In this article, we will learn how to insert multiple Key-Value pairs efficiently into a multimap. Example: Input: multi_map = {{"Manas","Singing" }, 2 min read How to Insert Pairs into a Map in C++? In C++, maps are STL containers that store key-value pairs and maintain the element's order according to their keys. Pairs also store the data as key values. In this article, we will discuss how to insert a pair into a map in C++. For Example, Input: map<int, string> mp = {(1, "geek"), (2,"for 2 min read How to Insert a Pair in Multimap in C++? In C++, we have multimap which is used to store key-value pairs like a map but multimap can have multiple values for the same key. In this article, we will learn how to insert a pair into a multimap in C++. Example Input: myMultimap = { {1, "this"}, {2,"is"}} myPair = {2, "was"}; Output: myMultimap 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 Initialize Multimap with Default Values in C++? In C++, a multimap is a container provided by the STL library that stores key-value pairs in an ordered manner. Unlike a map, a multimap allows multiple values to be associated with a single key. In this article, we will learn how to initialize a multimap with default values in C++. Initialize Multi 2 min read Like