mardi 19 janvier 2021

Is there a situation where only std::map can be used instead of std::unordered_map or vice versa?

std::map<int, int> counts;
// populate the map
for (auto& num : counts) {
  auto a = num.first;
  auto a_count = num.second;
  // do something useful
}

I first make counts a std::unordered_map, and strangely, cannot find a certain key, value that I am sure I put in it, while iterating through it. However, if I make it std::map, I can find the key and value in counts. Again, in both cases, the map is populated with the exact same values. What is the explanation for this behavior?

Aucun commentaire:

Enregistrer un commentaire