c++ - 如何查找std::map中是否存在元素?

标签 c++

我的用例:

map<string, Car> cars;
bool exists(const string& name) {
  // somehow I should find whether my MAP has a car
  // with the name provided
  return false;
} 

您能否建议用 C++ 实现它的最佳和最优雅的方法?谢谢。

最佳答案

return cars.find(name) != cars.end();

关于c++ - 如何查找std::map中是否存在元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2781899/

相关文章:

c++ - 我们需要为 gcnew 创建指针验证吗

C++ boost : Split function is_any_of()

c++ - 预期为 'double *',但参数的类型为 'double',并且函数的参数 2/3/4/5 的类型不兼容

c++ - 传递 opencv inputarray 并将其用作 std::vector

c++ - 无法在 Halide 中加载灰度图像

c++ - 在 Rcpp 中动态添加行

c++ - 嵌套类名在封闭类模板中使用时是否被视为当前实例化

c++ - 如何绕过转换 const char* 到 int

涉及 Bool 设置的 C++ 优化

c++ - [Qt] : 'CButton' does not name a type