for (auto i = myMap.begin(); i != myMap.end(); ++i) {
cout << i->first << ": " << i->second << endl;
}
#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 << "
";
}
}
// C++17 and above
for (auto const& [key, value] : map)
{
// do something
}
#include <iostream>
#include <map>
int main() {
std::map<std::string, int> myMap;
myMap["one"] = 1;
myMap["two"] = 2;
myMap["three"] = 3;
for ( const auto &[key, value]: myMap ) {
std::cout << key << '
';
}
}
it = map.begin();
++it;
for(; it != map.end(); ++it) {
// do something
}
//shorter one:
it = ++map.begin();
for (auto const& x : symbolTable)
{
std::cout << x.first // string (key)
<< ':'
<< x.second // string's value
<< std::endl;
}
// C++11 and onwards
for (auto const& keyValue : map)
{
keyValue.first; // Key
keyValue.second; // Value
}
for (auto i : m)
cout << i.first << " " << i.second
<< endl;
void output(map<string, int> table)
{
map<string, int>::iterator it;
for (it = table.begin(); it != table.end(); it++)
{
//How do I access each element?
}
}