Visual Studio Code 中的“std::map”类没有成员“contains”

Wik*_*tor 5 c++ visual-studio-code c++20

当我map.contains()使用 Visual Studio Code 在 C++ 代码中使用时,我收到以下消息:

类“std::map<int, int, std::less, std::allocator<std::pair<const int, int>>>”没有成员“包含”

值得庆幸的是,我的代码在运行时可以编译g++ -std=c++20 test.cc -o test,但 VSCode 一直告诉我有问题,这很烦人。这是我的代码:

#include <map>
using namespace std;

map<int, int> m;

bool contains_key(int idx) { return m.contains(idx); }
Run Code Online (Sandbox Code Playgroud)

有没有人遇到过同样的问题并且知道如何解决它?

Rem*_*eau 7

std::map::contains()在 C++20 中引入,这就是当您将编译配置为使用 C++20 时它可以工作的原因。

对于早期的 C++ 版本,您必须使用std::map::find()orstd::map::count()代替:

bool contains_key(int idx) { return m.find(idx) != m.end(); }
Run Code Online (Sandbox Code Playgroud)
bool contains_key(int idx) { return m.count(idx) > 0; }
Run Code Online (Sandbox Code Playgroud)