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 Must Do Coding Questions for Companies like Amazon, Microsoft, Adobe, ... gaurav472 Follow Improve Article Tags : C++ Programs C++ STL cpp-unordered_map cpp-pair CPP Examples +2 More Practice Tags : CPPSTL Similar Reads Interview Preparation Interview Preparation For Software Developers Must Coding Questions - Company-wise Must Do Coding Questions - Topic-wise Company-wise Practice Problems Company Preparation Competitive Programming Software Design-Patterns Company-wise Interview Experience Experienced - Interview Experiences Internship - Interview Experiences Practice @Geeksforgeeks Problem of the Day Topic-wise Practice Difficulty Level - School Difficulty Level - Basic Difficulty Level - Easy Difficulty Level - Medium Difficulty Level - Hard Leaderboard !! Explore More... Data Structures Arrays Linked List Stack Queue Binary Tree Binary Search Tree Heap Hashing Graph Advance Data Structures Matrix String All Data Structures Algorithms Analysis of Algorithms Searching Algorithms Sorting Algorithms Pattern Searching Geometric Algorithms Mathematical Algorithms Randomized Algorithms Greedy Algorithms Dynamic Programming Divide & Conquer Backtracking Branch & Bound All Algorithms Programming Languages C C++ Java Python C# Go Lang SQL PHP Scala Perl Kotlin Web Technologies HTML CSS JavaScript Bootstrap Tailwind CSS AngularJS ReactJS jQuery NodeJS PHP Web Design Web Browser File Formats Computer Science Subjects Operating Systems DBMS Computer Network Computer Organization & Architecture TOC Compiler Design Digital Elec. & Logic Design Software Engineering Engineering Mathematics Data Science & ML Complete Data Science Course Data Science Tutorial Machine Learning Tutorial Deep Learning Tutorial NLP Tutorial Machine Learning Projects Data Analysis Tutorial Tutorial Library Python Tutorial Django Tutorial Pandas Tutorial Kivy Tutorial Tkinter Tutorial OpenCV Tutorial Selenium Tutorial GATE CS GATE CS Notes Gate Corner Previous Year GATE Papers Last Minute Notes (LMNs) Important Topic For GATE CS GATE Course Previous Year Paper: CS exams Like