1//traditional way (long)
2for(map<string,int>::iterator it=m.begin(); it!=m.end(); ++it)
3 if(it->second)cout<<it->first<<" ";
4//easy way(short) just works with c++11 or later versions
5for(auto &x:m)
6 if(x.second)cout<<x.first<<" ";
7//condition is just an example of use
8
1map<string, int>::iterator it;
2
3for (it = symbolTable.begin(); it != symbolTable.end(); it++)
4{
5 std::cout << it->first // string (key)
6 << ':'
7 << it->second // string's value
8 << std::endl;
9}
10
1#include <iostream>
2#include <map>
3
4int main() {
5 std::map<int, float> num_map;
6 // calls a_map.begin() and a_map.end()
7 for (auto it = num_map.begin(); it != num_map.end(); ++it) {
8 std::cout << it->first << ", " << it->second << '\n';
9 }
10}
1//Since c++17
2for (auto& [key, value]: myMap) {
3 cout << key << " has value " << value << endl;
4}
5//Since c++11
6for (auto& kv : myMap) {
7 cout << kv.first << " has value " << kv.second << endl;
8}
1#include <iostream>
2#include <map>
3
4int main()
5{
6 std::map<std::string, int> myMap;
7
8 myMap["one"] = 1;
9 myMap["two"] = 2;
10 myMap["three"] = 3;
11
12 for ( const auto &myPair : myMap ) {
13 std::cout << myPair.first << "\n";
14 }
15}
16