for (auto i = myMap.begin(); i != myMap.end(); ++i) {
cout << i->first << ": " << i->second << endl;
}
#include <iostream>
#include <map>
int main() {
std::map<int, float> num_map;
// calls a_map.begin() and a_map.end()
for (auto it = num_map.begin(); it != num_map.end(); ++it) {
std::cout << it->first << ", " << it->second << '
';
}
}
#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
}
// 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?
}
}