Range-Based For Loop with a Map in C++ Last Updated : 28 Feb, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report In C++, a map is a container that stores elements formed by a combination of a key value and a mapped value. A range-based for loop is a feature added in C++11 to iterate over containers. In this article, we will learn how to use a range-based for loop with a map in C++. Example:Input:myMap: { {1, “John”}, {2, “Adam”} }Output:1: John2: AdamRange-Based for Loop with a Map in C++We can use the range-based for loop with a std::map to replace any other loop and simplify the code. The range-based for loop will take the name of the map and iterator over each element in the container. C++ Program to Traverse Map with Range Based for Loop C++ // C++ Program to Use a Range-Based For Loop with a Map #include <iostream> #include <map> using namespace std; // Driver Code int main() { // Decalarion and intitalization of the map with key of // type string and value of type int map<string, int> priceOfFruits = { { "Apple", 50 }, { "Mango", 30 }, { "Banana", 25 }, { "Orange", 20 } }; // Printing the key and values of the map cout << "Ranged-based for loop with read-only iterator" << endl; for (const auto eachPair : priceOfFruits) { cout << eachPair.first << ": " << eachPair.second << endl; } // Modifying the values of the map and then printing the // key and values of the map cout << "Ranged-based for loop with writable iterator" << endl; for (auto& eachPair : priceOfFruits) { eachPair.second += 10; cout << eachPair.first << ": " << eachPair.second << endl; } return 0; } OutputRanged-based for loop with read-only iterator Apple: 50 Banana: 25 Mango: 30 Orange: 20 Ranged-based for loop with writable iterator Apple: 60 Banana: 35 Mango: 40 Orange: 30 Time Complextiy: O(N logN), where N is the number of elements.Space Complexity: O(1) Comment More infoAdvertise with us Next Article Range-Based For Loop with a Map in C++ R rohan_paul Follow Improve Article Tags : C++ Programs C++ STL cpp-map CPP Examples +1 More Practice Tags : CPPSTL Similar Reads Range-Based for Loop with a Set in C++ In C++, a set is a container that stores unique elements in a particular order. A range-based for loop is a feature added in C++11 to iterate over containers. In this article, we will learn how to use a range-based for loop with a set in C++. Example:Input:mySet = {1, 2, 3, 4, 6}Output:1 2 3 4 6Rang 2 min read How to Use a Range-Based for Loop with a Vector in C++? C++ introduced the range-based for loop as a convenient way to iterate over elements in a container. In this article, we'll look at how to iterate over a vector using a range-based loop in C++. Example Input:myVector: {1, 2, 3, 4, 5}Output:1 2 3 4 5Range-Based for Loop with Vector in C++The syntax o 1 min read Reversed Range-based for loop in C++ with Examples Range-based for loops is an upgraded version of for loops. It is quite similar to for loops which is use in Python. Range-based for loop in C++ is added since C++ 11. We can reverse the process of iterating the loop by using boost::adaptors::reverse() function which is included in boost library Head 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 Loop Over an Array in C++? In C++, an array is a data structure that stores elements of similar type in contiguous memory locations. We can access the elements of an array using array indexing. In this article, we will learn how to loop over an array in C++. Example: Input: int arr[] = [1, 2, 3, 4, 5] Output: Array Elements: 2 min read Like