c++ - 为什么 C++ 映射中没有 "exists"函数?

标签 c++ dictionary language-features

我发现自己有时会检查 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, otherwise false.

if(myMap.contains(item)) ...

关于c++ - 为什么 C++ 映射中没有 "exists"函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66133976/

相关文章:

c++ - 什么是奇怪的重复模板模式 (CRTP)?

python - 为文件夹中的文件生成字典元素列表

C++11 特性检查

python-3.x - 我想将字典转换为 pandas dataFrame

programming-languages - 有更好的C吗?

Python 字典详细信息

c++ - 错误 : conversion from 'const char [10]' to non-scalar type 'std::fstream {aka std::basic_fstream<char>}' requested

C++ ifstream 在从文本文件读取时附加垃圾数据

c++ - 写入未打开的 std::ofstream

python - 创建一个具有未知键的字典并向其附加一个列表值?