Q:

Using a map iterator write an expression that assigns a value to an element

0

Using a map iterator write an expression that assigns a value to an element.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

#include <map>
#include <iostream>

int main() {
  std::map<int, int> mi;
  int a, b;
  while (std::cin >> a >> b) {
    auto it = mi.find(a);
    if (it != mi.end())
      it->second = b;
    else
      mi.insert({a, b});
  }
  for (auto it = mi.cbegin(); it != mi.cend(); ++it)
    std::cout << it->first << " : " << it->second << std::endl;

  return 0;
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now