How to check if std::map contains a key without doing insert?

后端 未结 3 1589
遥遥无期
遥遥无期 2020-12-04 08:26

The only way I have found to check for duplicates is by inserting and checking the std::pair.second for false, but the problem is that this still i

3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-04 09:03

    Use my_map.count( key ); it can only return 0 or 1, which is essentially the Boolean result you want.

    Alternately my_map.find( key ) != my_map.end() works too.

提交回复
热议问题