如何找到一个元素是否存在于std :: map? [英] How to find whether an element exists in std::map?

查看:1382
本文介绍了如何找到一个元素是否存在于std :: map?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的使用案例:

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 + ?非常感谢。

Could you please suggest the best and the most elegant way to do it in C++? Thanks.

推荐答案

当然,使用迭代器

map<string,Car>::const_iterator it = cars.find(name);
return it!=cars.end();

这篇关于如何找到一个元素是否存在于std :: map?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆