
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to use range-based for() loop with std::map?
In C++, a map is defined by an element storage container in the form of key-value pairs where each key is unique and mapped with a respective value. While range-based for loop is a very simple approach to iterate over elements in the container. In this article, we will learn the usage of range-based for loop over std::map in C++.
Example Scenario
Input: myMap: { {1, "Ravi"}, {2, "Tom"} } Output: 1: Ravi 2: Tom
What is std::map?
The std::map is an associative container that stores key-value pairs in sorted order. The maps are used for fast lookup, insertion, and deletion based on keys.
Following is the basic syntax of map in C++ STL:
map<key_type, value_type, comp> m;
Here,
- key_type: This is data type of key.
- value_type: This is data type of value.
- comp: This is optional if you want to use then use it as it compares two keys of sorting.
- m: This is the name assigned to map.
Range-based for Loop
A range-based for loop allows you to iterate over elements of a container or array directly.
Below is the basic syntax of range-based for loop:
for ( range_declaration : range_expression ) { // block of code }
Example to Use Range-based for() Loop with std::map
In this example, we use the map data structure, which is a part of the C++ STL. A map stores key-value pairs, where each key is unique. In this case, we insert characters as keys and strings as values. These key-value pairs form are printed with the help of range-based for loop:
#include<iostream> #include<map> int main() { // Declaration of map // where each key is a char and each value is a string std::map<char, std::string> my_map; // insert key-value pairs into the map my_map.insert(std::pair<char, std::string>('A', "Apple")); my_map.insert(std::pair<char, std::string>('B', "Ball")); my_map.insert(std::pair<char, std::string>('C', "Cat")); my_map.insert(std::pair<char, std::string>('D', "Dog")); my_map.insert(std::pair<char, std::string>('E', "Elephant")); my_map.insert(std::pair<char, std::string>('F', "Fox")); my_map.insert(std::pair<char, std::string>('G', "Ghost")); my_map.insert(std::pair<char, std::string>('H', "Horse")); my_map.insert(std::pair<char, std::string>('I', "India")); my_map.insert(std::pair<char, std::string>('J', "Jungle")); // range-based for loop for(auto& key_val : my_map) { std::cout << "The " << key_val.first << " is pointing to: " << key_val.second << std::endl; } return 0; }
The above code produces the following result:
The A is pointing to: Apple The B is pointing to: Ball The C is pointing to: Cat The D is pointing to: Dog The E is pointing to: Elephant The F is pointing to: Fox The G is pointing to: Ghost The H is pointing to: Horse The I is pointing to: India The J is pointing to: Jungle