How to Find First Key-Value Pair in a Map in C++? Last Updated : 04 Mar, 2024 Comments Improve Suggest changes Like Article Like Report In C++ STL, a map is a container that stores key-value pairs in an ordered or sorted manner. In this article, we will learn how to find the first key-value pair in a Map. Example: Input: myMap = {{1, "C++"}, {2, "Java"}, {3, "Python"}, {4, "JavaScript"}}; Output: First key-Value Pair : (1, C++)Getting First Key-Value Pair in a MapTo find the first key-value pair in a C++ map, we can use the std::map::begin() function that returns an iterator pointing to the first element in the map. We can dereference this iterator to access the key and value or we can also use the arrow operator(->) to directly access the key and value. C++ Program to Find the First Key-Value Pair in a Map C++The below example demonstrates how we can use the std::map::begin() to find the first key-value pair in a map in C++ STL. C++ // C++ program to illustrate how to find the first key-value // pair in a Map #include <iostream> #include <map> using namespace std; int main() { // Create a map with integer keys and string values map<int, string> myMap = { { 1, "C++" }, { 2, "Java" }, { 3, "Python" }, { 4, "JavaScript" } }; // Using the begin() function to get an iterator to the // first element auto firstElement = myMap.begin(); // Access the key and value using the iterator cout << "First key-Value Pair in a map : {" << firstElement->first << ", " << firstElement->second << '}' << endl; return 0; } OutputFirst key-Value Pair in a map : {1, C++} Time Complexity: O(1)Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article How to Find First Key-Value Pair in a Map in C++? subhra_ghosh Follow Improve Article Tags : C++ Programs C++ STL cpp-map CPP Examples +1 More Practice Tags : CPPSTL Similar Reads How to Delete a Key-Value Pair from a Map in C++? In C++, maps are used to store key-value pairs in which each key is unique. In this article, we will learn how to delete a key-value pair from a map in C++. Example Input: mp={ {1,"One"}, {2,"Two"},{3,"Three"}}Key= 2Output: Map after deleting key: 1: One3: ThreeRemove a Key-Value Pair from Map in C+ 2 min read How to Remove a Key-Value Pair from Map in C++? In C++, a map is an associative container that stores elements in key-value pairs, where the key values are unique. In this article, we will learn how to remove a specific key-value pair from a map in C++. Example: Input : myMap = {{1, 10}, {3, 30}, {2, 20}, {4, 40}};key = 2Output :Map After Removal 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 Find the Maximum Key in a Map in C++? In C++, the Standard Template Library (STL) provides a map container to store elements in a mapped fashion so each element has a key value and a mapped value. In this article, we will see how to find the maximum key in a map in C++. Example: Input : myMap : {{1, 10}, {2, 20}, {4, 12}, {3, 44}}Output 2 min read How to Delete Multiple Key-Value Pairs from a Map in C++? In C++, a map container stores the collection of data in the form of a key and a value pair and this data is sorted on the basis of the key. In this article, we will learn how to delete multiple key-value pairs from a map in C++ STL. Example: Input: myMap = {{âappleâ, 1}, {âbananaâ, 2}, {âcherryâ, 3 2 min read Like