What is the difference between std::unordered_map<std::string, int> and std::unordred_map<const std::string, int>?

Viewed 96

What is the difference between std::unordered_map<std::string, int> and std::unordred_map<const std::string, int>?

I found that if I use std::unordred_map<const std::string, int>, it fails to compile.

std::unordered_map<const std::string, int> m;
m.insert(std::make_pair("Foo", 1)); //error
m["Bar"] = 2; // also error

But std::unordered_map<std::string, int> works fine.

I think std::unordred_map<const std::string, int> should be fine, because the key of unordered_map is always const. But it's not working. Why is that?

0 Answers
Related