我发现自己有时会检查 std::map
中是否已存在某个项目,我使用以下方法执行此操作:
if(myMap.find(item) != myMap.end()) ...
我想知道为什么没有像 exists()
这样的函数可以返回相同的 bool 值来判断该项目是否已经在 map 中。
这会节省一些打字时间,但更重要的是它看起来更加清晰:
if(myMap.exists(item)) ...
最佳答案
从 C++20 开始,您可以使用 contains
.
Return value
true
if there is such an element, otherwisefalse
.
if(myMap.contains(item)) ...
关于c++ - 为什么 C++ 映射中没有 "exists"函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66133976/