What happens if I read a map's value where the key does not exist?

前端 未结 7 1095
孤独总比滥情好
孤独总比滥情好 2020-12-05 12:24
map dada;
dada[\"dummy\"] = \"papy\";
cout << dada[\"pootoo\"];

I\'m puzzled because I don\'t know if it\'s considered

相关标签:
7条回答
  • 2020-12-05 13:21

    please have a look at the out_of_range exception: http://www.cplusplus.com/reference/stdexcept/out_of_range/

    this is what map::at and map::operator[] will throw if key does not exist. You can catch it the same way as the vector example in the link.

    You can also use: http://www.cplusplus.com/reference/map/map/find/

    And check against map::end

    0 讨论(0)
提交回复
热议问题