How to Traverse a Map Using Iterator in C++? Last Updated : 04 Mar, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report 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++. Example :Input: myMap[1] = "One"; myMap[2] = "Two"; myMap[3] = "Three"; myMap[4] = "Four"; Output: Here is the map : { {1, One}, {2, Two}, {3, Three}, {4, Four} }Traverse a Map Using Iterator in C++To traverse a std::map using an iterator, we can use the std::map::begin() function to get an iterator pointing to the first element of the map and the std::map::end() function to get an iterator pointing one past the last element of the map. Then we can increment the iterator in a loop to traverse through the elements. C++ Program to Traverse a Map Using an Iterator C++ // C++ Program to illustrate how to use iterator to traverse // a map #include <iostream> #include <map> using namespace std; int main() { // Create a map with some key-value pairs map<int, string> myMap; myMap[1] = "One"; myMap[2] = "Two"; myMap[3] = "Three"; myMap[4] = "Four"; myMap[5] = "Five"; // Using iterator to traverse the map map<int, string>::iterator it; cout << "Here is the map : {" << endl; // using begin() and end() iterator to iterate // through the map for (it = myMap.begin(); it != myMap.end(); ++it) { cout << " {" << it->first << ", " << it->second << "} " << endl; } cout << '}' << endl; cout << "One full iteration complete."; return 0; } OutputHere is the map : { {1, One} {2, Two} {3, Three} {4, Four} {5, Five} } One full iteration complete.Time Complexity: O(n), where n is the number of elements in the map.Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article How to Traverse a Set with const_iterator in C++? S subhra_ghosh Follow Improve Article Tags : C++ Programs C++ cpp-iterator STL cpp-map CPP Examples +2 More Practice Tags : CPPSTL Similar Reads 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 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 Traverse a Set with const_iterator in C++? In C++, sets are a type of associative container in which each element has to be unique because the value of the element identifies it. It contains a constant iterator that provides the constant reference to its elements. In this article, we will discuss how to traverse a set with const_iterator in 2 min read How to Traverse a List with const_iterator in C++? In C++, a list is a container used to store data in non-contiguous memory locations. It also provides a constant iterator that provides the constant reference to its elements. In this article, we will discuss how to traverse a list with const_iterator in C++. Example Input: myList = {10,20,30,40,50} 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 Traverse Vector Using const_reverse_iterator in C++? In C++, a vector can be traversed in reverse order using a const_reverse_iterator. A const_reverse_iterator is a type of iterator that points to the last element in the container and moves in the reverse direction. In this article, we will learn how to traverse a vector using a const_reverse_iterato 2 min read Like