How to Insert a Pair into an Unordered Map in C++? Last Updated : 13 Feb, 2024 Comments Improve Suggest changes Like Article Like Report In C++, STL provides the pair container which allows the user to store two objects that can be of the same or different type as a single unit. On the other hand, the Unordered Map is a data structure that stores the data in the form of key-value pairs where the keys must be unique. In this article, we will learn how we can insert a Pair into an Unordered Map in C++. Example: Input: myUnorderedMap = { {1,"Geek"}, {2, "for"} }Output:myUnorderedMap= { {1,"Geek"}, {2, "for"}, {3,"Geeks"} }// pairs inserted into the unordered mapInsert a Pair into an Unordered Map in C++To insert a std::pair into a std::unordered map in C++, we can simply use the std::unordered_map::insert() method to insert the pair into the unordered map. It is a member function of the std::unordered_map class and only need to pass the said pair as an argument to it. Note: We must ensure the pair data type matches with the data type of the unordered map. C++ Program to Insert a Pair into an Unordered Map C++ // C++ program to insert a pair into an unordered map #include <iostream> #include <unordered_map> using namespace std; int main() { // declare an unordered map unordered_map<int, string> mp = { { 1, "Geek" }, { 2, "for" } }; // Create the pairs you want to insert pair<int, string> pair3 = make_pair(3, "Geeks"); // Insert the pairs into the unordered map using // insert() function mp.insert(pair3); // Print the unordered map for (auto& pair : mp) { cout << pair.first << ": " << pair.second << endl; } return 0; } Output3: Geeks 2: for 1: Geek Time Complexity: O(1), worst case O(n), where n is the number of elements in the unordered_pairAuxilary Space: O(1) Comment More infoAdvertise with us Next Article How to Insert a Pair into an Unordered Map in C++? G gaurav472 Follow Improve Article Tags : C++ Programs C++ STL cpp-unordered_map cpp-pair CPP Examples +2 More Practice Tags : CPPSTL Similar Reads 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 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 Delete a Pair from an Unordered Map in C++? In C++, the unordered_map is like a dictionary that stores data in the form of key-value pairs. In this article, we will learn how to delete a key-value pair from an unordered_map in C++. Example Input:mp={ {1,"Apple"}, {3,"Mango"},{2,"Orange"}}Key= 3Output:Map after deleting key:1: Apple2: OrangeRe 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 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 Like