How to Use const_iterator with a Map in C++? Last Updated : 29 Feb, 2024 Comments Improve Suggest changes Like Article Like Report 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}, {“banana”, 2}, {“cherry”, 3}}Output: result: {{“apple”, 1}, {“banana”, 2}, {“cherry”, 3}}Iterator Over a Map Using const_iterator in C++We can get the const_iterator to a std::map container using the std::map::cbegin() and std::map::cend() functions that return a const_iterator pointing to the beginning and the end of the map, respectively. These constant iterators are recommended when we do not want to modify the data in the map C++ Program to Iterator Over a Map Using const_iterator C++ // C++ Program to illustrate how to Iterator Over a Map // Using const_iterator #include <iostream> #include <map> using namespace std; // Driver Code int main() { // Creating a map of string and int map<string, int> myMap = { { "apple", 1 }, { "banana", 2 }, { "cherry", 3 } }; // Declaring a const_iterator to the map map<string, int>::const_iterator it; // Displaying map elements using const_iterator for (it = myMap.cbegin(); it != myMap.cend(); ++it) { cout << it->first << " " << it->second << endl; } return 0; } Outputapple 1 banana 2 cherry 3 Time Complexity: O(N), where N is the number of elements in the map.Space Complexity: O(1) Comment More infoAdvertise with us Next Article How to Use const_iterator with a Map in C++? D denzirop9v Follow Improve Article Tags : C++ Programs C++ cpp-iterator STL cpp-map CPP Examples +2 More Practice Tags : CPPSTL Similar Reads 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 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 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 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 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 Like