How to Access Value in a Map Using Iterator in C++? Last Updated : 29 Feb, 2024 Comments Improve Suggest changes Like Article Like Report In C++, a map is a container that stores elements in the form of a key value and a mapped value pair. In this article, we will learn how to access a value in a map using an iterator in C++. Accessing Value in a Map Using Iterator in C++To access a value associated with a key in a std::map using an iterator, we can use the std::map::find() function to get an iterator to the element with the specified key. If the key is not found, the function returns an iterator to the end. C++ Program to Access Value in a Map Using Iterator C++ // C++ Program to access a value in a map using an iterator #include <iostream> #include <map> using namespace std; int main() { // Creating a map of string and int map<string, int> myMap = { { "apple", 1 }, { "banana", 2 }, { "cherry", 3 } }; // Getting an iterator to the element with the key // "banana" map<string, int>::iterator it = myMap.find("banana"); // Checking if the key was found if (it != myMap.end()) { // Accessing the value using the iterator cout << "The value of the key 'banana' is " << it->second << endl; } else { cout << "The key 'banana' was not found in the map" << endl; } return 0; } OutputThe value of the key 'banana' is 2 Time Complexity: O(log N), where N is the size of the map.Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article How to Access Value in a Map Using Iterator in C++? R rohitpant4532 Follow Improve Article Tags : C++ Programs C++ cpp-iterator STL cpp-map CPP Examples +2 More Practice Tags : CPPSTL Similar Reads How to Access a Value in a Map Using a Key in C++? In C++, std::map container store data in the form of key-value pairs where keys are associated with the given value. In this article, we will learn how to access a value in a std::map using key in C++.ExamplesInput: m = {{'a', 11}, {'b', 45}, {'c', 9}}, k = 'c'Output: 9Explanation: The key 'c' prese 3 min read How to Access First Element in a Vector Using Iterator in C++? In C++, vectors are containers similar to arrays but unlike arrays, vectors can resize themselves during the runtime. These vectors provide iterators to access their elements. In this article, we will learn how to access the first element in a vector using an iterator in C++. Example Input:myVector 2 min read How to Access Vector Element Using Iterator in C++? In C++, vectors are container that works like a dynamic array. STL provides iterators for the traversal of these containers. In this article, we will learn how to access an element in a vector using an iterator in C++. Example Input: myVector = {1, 5, 8, 1, 3, 4} Output: myVector value at index 5 us 2 min read How to Traverse a Map Using Iterator in C++? In C++, maps are associative containers that store elements where each element has a key value and a mapped value. An iterator is a pointer-like object that allows traversing through the elements of a container like a map. In this article, we will learn how to traverse a map using an iterator in C++ 2 min read How to Use const_iterator with a Map in C++? In C++, a const_iterator is a type of iterator that points to constant content. This means that using a const_iterator, you can read from but not write to the element it points to. In this article, we will learn how to use a const_iterator with a map in C++ STL. Example: Input: myMap = {{âappleâ, 1} 2 min read How to Use a reverse_Iterator with a Map in C++? In C++, a reverse_iterator is a type of iterator that points to elements in a container in the reverse order. This means that using a reverse_iterator, you can traverse a container from end to beginning. In this article, we will learn how to use a reverse_iterator with a map in C++. Example: Input: 2 min read How to Insert into Vector Using Iterator in C++? In C++, a vector is a dynamic array that can grow and shrink in size as needed. Vectors are sequence containers representing arrays that can change in size. In this article, we will learn how to insert elements into a vector using an iterator in C++. Example: Input: myVector = {10,20,30}; Output: Ve 2 min read How to Update Value of a Key in Map in C++? In C++, a map is a data structure that stores key-value pairs. In this article, we will learn how to change the value of a key that is already present in the map container in C++. Example Input: map<int, string> mp = {(1, "One"), (2,"Two")} Output: map after Updation: {(1, "One"), (2, "Three") 2 min read How to Use Iterator with a Vector in C++? In C++, vectors are the same as dynamic arrays with the ability to resize themselves automatically when an element is inserted or deleted. An iterator is a pointer-like object that can be used to access elements of a container (like an array or a vector). In this article, we will learn how to use an 2 min read How to Traverse Vector using const_iterator in C++? In C++, a const_iterator is a type of iterator that points to constant content means by using a const_iterator, we can read from but cannot write to the element it points to. In this article, we will learn how to use a const_iterator with a vector in C++ STL. Example: Input: myVector = {1, 2, 3} Out 2 min read Like