mercredi 1 avril 2020

Cannot iterate map through auto in C++

I am trying to iterate this simple through auto feature but it is giving me tons of errors. I don't know what's wrong with this code.

#include <vector>
#include <unordered_map>
#include <iostream>
using namespace std;

int main() {
  std::unordered_map<std::string, int> m = {
      {"apples", 5},
      {"bananas", 3},
      {"pears", 7},
  };
  for (auto& [fruit, count] : m) { //line 13
    std::cout << "I have " << count << " " << fruit << ".\n";
  }
}

13 [Error] expected unqualified-id before '[' token13

14 [Error] expected ';' before '[' token

13 [Error] 'fruit' was not declared in this scope

13 [Error] 'count' was not declared in this scope

And many more errors. I have read this example online and it is working fine there.

Aucun commentaire:

Enregistrer un commentaire