loop map c++ code example
Example 1: how to iterate through a map in c++
for(map<string,int>::iterator it=m.begin(); it!=m.end(); ++it)
if(it->second)cout<<it->first<<" ";
for(auto &x:m)
if(x.second)cout<<x.first<<" ";
Example 2: 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 3: 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;
}