Map iterator in c++

To illustrate the function, consider the example below which explains how to iterate through a C++ map using a map iterator. The question is about iterating through the keys of a map while also looking ahead to future keys.

c++
map iterator
#include 
#include 
 
int main() {
  std::map 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 << 'n';
  }
}

c++
iterate map
// C++11 and onwards
for (auto const& keyValue : map)
{
  keyValue.first; // Key
  keyValue.second; // Value
}

Frequently Asked Questions

Posted in Uncategorized