How to Insert Pairs into a Map in C++? Last Updated : 29 Jan, 2024 Comments Improve Suggest changes Like Article Like Report 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")} pair<int, string> pr = {3, "geeks"} Output: // after inserting pr to mp mp = {(1, "geek"), (2,"for"), (3, "geeks")}Insert Pair into a Map in C++Pairs can be inserted directly into the map container by using the std::map::insert() function. We just need to pass the said pair as an argument to this function. C++ Program to Insert Pair into a Map C++ // C++ program to demonstrate the insertion of pairs into // map using insert() function. #include <iostream> #include <map> using namespace std; int main() { // Step 1: Create a map with integer keys and string // values. map<int, string> myMap; // Step 2: Create a pair to insert. pair<int, string> newPair(25, "GeeksforGeeks"); // Step 3: Insert the pair into the map using the // insert() function. myMap.insert(newPair); // Iterate over the map and print each key-value pair. cout << "Contents of the map:" << endl; for (const auto& pair : myMap) { cout << "Key: " << pair.first << ", Value: " << pair.second << endl; } return 0; } OutputContents of the map: Key: 25, Value: GeeksforGeeks We can also use std::map::emplace() method to insert pairs into a map instead of insert(). Comment More infoAdvertise with us Next Article How to Insert Pairs into a Map in C++? H heysaiyad Follow Improve Article Tags : C++ Programs C++ STL cpp-map cpp-pair CPP Examples +2 More Practice Tags : CPPSTL Similar Reads 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 Insert into Multimap using make_pair in C++? 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, âJ 2 min read 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 Iterate Over a Set of Pairs in C++? In C++, a set is a container that stores unique values in some specified order while pairs are data structures that store key-value pairs. In this article, we will look at how we can iterate through a set of pairs in C++. For Example, Input: mySet = { {100: "Geek"}, {200:" for"}, {300:" Geeks"} } Ou 2 min read How to Insert an Element into a Multiset in C++? In C++, multisets are associative containers similar to sets, but unlike sets, they allow the users to store duplicate elements. In this article, we will learn how we can insert an element into a multiset in C++. Example: Input: myMultiset ={1,2,4,5,6,7,8} Output: myMultiset = {1,2,3,4,5,6,7,8} // i 2 min read Like