[英]contains is not a member of std::map MSVC
在启用/std:c++latest
尝试在 Visual Studio 2017 中使用 c++ 20 功能std::map::contains() 时,编译器仍然无法找到 std::map 的 contains() 成员。 这还不适用于 MSVC 还是我做错了什么? 基本示例:
#include <iostream>
#include <map>
int main()
{
std::map<int,char> example = {{1,'a'},{2,'b'}};
if(example.contains(2)) {
std::cout << "Found\n";
} else {
std::cout << "Not found\n";
}
}
结果是:
>main.cpp
1>c:\dev\random_cpp\random_cpp\main.cpp(11): error C2039: 'contains': is not a member of 'std::map<int,char,std::less<int>,std::allocator<std::pair<const _Kty,_Ty>>>'
1> with
1> [
1> _Kty=int,
1> _Ty=char
1> ]
根据Microsoft C++ 语言一致性表,方法std::map::contains
仅在 VS 2019 16.1 后可用。
它尚未在 VS2017 附带的 Microsoft STL 版本中实现。 但是您可以使用 std::map::count() 模拟它 - 如果 count() 返回非零,则键存在,如果为零 - 不存在。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.