C++ 如何在 std::map 中查找元素是否存在?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2781899/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to find whether an element exists in std::map?
提问by yegor256
My use case:
我的用例:
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;
}
Could you please suggest the best and the most elegant way to do it in C++? Thanks.
您能否建议在 C++ 中最好和最优雅的方法?谢谢。
回答by kennytm
return cars.find(name) != cars.end();
回答by Tom
Sure, use an iterator
当然,使用迭代器
map<string,Car>::const_iterator it = cars.find(name);
return it!=cars.end();
回答by Johannes Schaub - litb
You could also use
你也可以使用
bool exists(const string& name) {
return cars.count(name) != 0;
}
回答by foo
Apart from the answers with iterator-Value from find() and comparison to .end(), there is another way: map::count.
除了 find() 的 iterator-Value 答案和与 .end() 的比较之外,还有另一种方法:map::count。
You can call map::count(key) with a specific key; it will return how many entries exist for the given key. For maps with unique keys, the result will be either 0 or 1. Since multimap exists as well with the same interface, better compare with != 0 for existence to be on the safe side.
您可以使用特定键调用 map::count(key) ;它将返回给定键存在多少条目。对于具有唯一键的映射,结果将是 0 或 1。由于 multimap 也存在于相同的接口中,为了安全起见,最好与 != 0 进行比较。
for your example, that's
对于你的例子,那就是
return (cars.count(name)>0);
The advantages I see are 1. shorter code, 2. benefit from whatever optimisations the library may apply internally, using its representation details.
我看到的优点是 1. 更短的代码,2. 受益于库可能在内部应用的任何优化,使用其表示细节。
回答by D.Shawley
What about:
关于什么:
template <typename KeyType, typename Collection>
bool exists_in(Collection const& haystack, KeyType const& needle) {
return std::find(haystack.begin(), haystack.end(), needle) != haystack.end();
}
template <typename K, typename V>
bool exists_in(std::map<K,V> const& haystack, K const& needle) {
return haystack.find(needle) != haystack.end();
}
This makes exists_in
work with any standard container via std::find
and use a special version for std::map
since it offers a more efficient searching alternative. You could add additional specializations as necessary (e.g., for std::set
and others).
这使得exists_in
可以通过任何标准容器使用std::find
并使用特殊版本,std::map
因为它提供了更有效的搜索替代方案。您可以根据需要添加其他专业化(例如,forstd::set
和其他)。
回答by Mike Seymour
bool exists(const string& name)
{
return cars.find(name) != cars.end();
}
回答by Brian Roach
std::map::find(const key_type& x );
std::map::find(const key_type& x );
It returns map::end
if the item doesn't exist.
map::end
如果该项目不存在,则返回。
回答by Lyberta
C++20:
C++20:
return cars.contains(name);
回答by Rudi
bool exists(const std::map<std::string, Car>& cars, const std::string& name) {
return cars.end() != cars.find(name);
}
回答by Dr.Vendetta
#define itertype(v) typeof((v).begin())
itertype(cars) it = cars.find(name);
return it != cars.end();