how to iterate over a map cpp code example
Example 1: through map c++
for (auto& [key, value]: myMap) {
cout << key << " has value " << value << endl;
}
for (auto& kv : myMap) {
cout << kv.first << " has value " << kv.second << endl;
}
Example 2: cpp goiver all the map values
map<string, int>::iterator it;
for (it = symbolTable.begin(); it != symbolTable.end(); it++)
{
std::cout << it->first
<< ':'
<< it->second
<< std::endl;
}
Example 3: cpp map iterate over keys
#include <iostream>
#include <map>
int main()
{
std::map<std::string, int> myMap;
myMap["one"] = 1;
myMap["two"] = 2;
myMap["three"] = 3;
for ( const auto &myPair : myMap ) {
std::cout << myPair.first << "\n";
}
}