I thought the only difference between std::unordered_map::operator[]
and std::unordered_map::at
is that std::unordered_map::at
throws std::out_of_range
if the key doesn't exist.
However, in the following code snippet, std::unordered_map::at
works but std::unordered_map::operator[]
gives a compiler error
const unordered_map<char, string> digit2Letters {
{'2', "abc"},
{'3', "def"},
{'4', "ghi"},
};
digit2Letters.at('1') // works good
digit2Letters['1']; // compiler error: no operator "[]" matches these operands
Does anyone know why std::unordered_map::at
works but std::unordered_map::operator[]
fails?
Aucun commentaire:
Enregistrer un commentaire