在C++中有模拟IDictionary.ContainsKey(TKey)或List.Contains(T)吗?C++模拟ContainsKey(TKey)来自C# - 最好的实践是什么?
例如我有数组元素,需要知道这个数组有一些值或不是? 什么是最好的方式或最好的实践 - 没有每个元素的“foreach”! 这将是很好,如果它会从std库例如。
UPD 1:在标准库有很多容器,但我想找到一个最好的方式 - 更快,少的代码,那么复杂,等等...
Lookind是更好desigion被std::unordered_set
如果this logic
#include <unordered_set>
std::unordered_set<std::string> NamesOfValues = {
"one",
"two",
"Date",
"Time"
};
// and now check is value exists in set
if(NamesOfValues.count(value))
{
// value exists
}
可以看看std :: map(特别是 - find()函数):http://www.cplusplus.com/reference/map/map/find/ –
可能吧,我看看 - 但可能有更好的指示 - 例如std :: map :: count看起来更好... – Alexander
[确定映射是否包含某个键的值?]的可能重复(https://stackoverflow.com/questions/3136520 /确定,如果地图,包含一个值为一个键) –