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++? 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 How to Create a Stack of Unordered_Map in C++? In C++, the stack is a container that follows the LIFO(Last In First Out) rule where new elements are added from one end (top) and removed from that end only. An unordered_map is an associative container that stores elements formed by a combination of key-value pairs, where the key should be unique. 2 min read How to Replace a Key-Value Pair in a Map in C++? A map in C++ is a part of the Standard Template Library (STL) that allows the user to store data in key-value pairs where each key in a map must be unique. In this article, we will learn how to replace a specific key-value pair in a Map in C++. Example: Input : map<int, string> mp={{1,"John"}, 2 min read How to Sort a Vector in a Map in C++? In C++, we can create a map container where the values associated with keys is a vector. In this article, we will learn how to sort a vector within a map in C++. Example Input: myMap = { {3, {9, 7, 3}}, {5, {4, 2, 8, 1, 6}}, {8, {1, 2, 5, 8}} }; Output: Map: Key: 3, Sorted Vector: [3 7 9 ] Key: 5, S 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 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