检查 std::map 中是否存在 - 计数与查找
Checking for existence in std::map - count vs find
所以似乎有两种普遍接受的方法来确定std::map
中是否存在键:
map.find(key) != map.end()
map.count(key) > 0
一个比另一个更有效率吗?具体来说,count()
的概念可以解释为该方法将迭代每个键,计算总数(并且由于std::map
的定义,该总数将始终为 0 或 1(。count()
能保证在比赛后"停止",以与find()
相同的复杂度运行吗?
由于地图最多只能有一个键,因此在找到一个元素后,count
基本上会停止。但是,鉴于更通用的容器(例如 multimap 和 multiset(,如果您只关心具有此键的某个元素是否存在,则严格来说find
更好,因为一旦找到第一个匹配元素,它就可以真正停止。
通常,count
和 find
都将使用特定于容器的查找方法(树遍历或哈希表查找(,这些方法始终相当有效。只是count
必须继续迭代直到相等范围的结束,而find
则没有。此外,您的代码应该记录意图,因此如果您想查找某些内容,请使用 find
.
根据源代码,我建议使用 find
.请参阅源代码。
在 GCC 中,代码如下 ( stl_map.h
(:
const_iterator
find(const key_type& __x) const
{ return _M_t.find(__x); }
size_type
count(const key_type& __x) const
{ return _M_t.find(__x) == _M_t.end() ? 0 : 1; }
在Windows平台上的Visual Studio中,代码如下(xtree
(:
const_iterator find(const key_type& _Keyval) const
{ // find an element in nonmutable sequence that matches _Keyval
const_iterator _Where = lower_bound(_Keyval);
return (_Where == end()
|| _DEBUG_LT_PRED(this->_Getcomp(),
_Keyval, this->_Key(_Where._Mynode()))
? end() : _Where);
}
//....
const_iterator lower_bound(const key_type& _Keyval) const
{ // find leftmost node not less than _Keyval in nonmutable tree
return (const_iterator(_Lbound(_Keyval), this));
}
//....
_Nodeptr _Lbound(const key_type& _Keyval) const
{ // find leftmost node not less than _Keyval
_Nodeptr _Pnode = _Root();
_Nodeptr _Wherenode = this->_Myhead; // end() if search fails
while (!this->_Isnil(_Pnode))
if (_DEBUG_LT_PRED(this->_Getcomp(), this->_Key(_Pnode), _Keyval))
_Pnode = this->_Right(_Pnode); // descend right subtree
else
{ // _Pnode not less than _Keyval, remember it
_Wherenode = _Pnode;
_Pnode = this->_Left(_Pnode); // descend left subtree
}
return (_Wherenode); // return best remembered candidate
}
//..........................................
//..........................................
size_type count(const key_type& _Keyval) const
{ // count all elements that match _Keyval
_Paircc _Ans = equal_range(_Keyval);
size_type _Num = 0;
_Distance(_Ans.first, _Ans.second, _Num);
return (_Num);
}
//....
_Pairii equal_range(const key_type& _Keyval) const
{ // find range equivalent to _Keyval in nonmutable tree
return (_Eqrange(_Keyval));
}
//....
_Paircc _Eqrange(const key_type& _Keyval) const
{ // find leftmost node not less than _Keyval
_Nodeptr _Pnode = _Root();
_Nodeptr _Lonode = this->_Myhead; // end() if search fails
_Nodeptr _Hinode = this->_Myhead; // end() if search fails
while (!this->_Isnil(_Pnode))
if (_DEBUG_LT_PRED(this->_Getcomp(), this->_Key(_Pnode), _Keyval))
_Pnode = this->_Right(_Pnode); // descend right subtree
else
{ // _Pnode not less than _Keyval, remember it
if (this->_Isnil(_Hinode)
&& _DEBUG_LT_PRED(this->_Getcomp(), _Keyval,
this->_Key(_Pnode)))
_Hinode = _Pnode; // _Pnode greater, remember it
_Lonode = _Pnode;
_Pnode = this->_Left(_Pnode); // descend left subtree
}
_Pnode = this->_Isnil(_Hinode) ? _Root()
: this->_Left(_Hinode); // continue scan for upper bound
while (!this->_Isnil(_Pnode))
if (_DEBUG_LT_PRED(this->_Getcomp(), _Keyval, this->_Key(_Pnode)))
{ // _Pnode greater than _Keyval, remember it
_Hinode = _Pnode;
_Pnode = this->_Left(_Pnode); // descend left subtree
}
else
_Pnode = this->_Right(_Pnode); // descend right subtree
const_iterator _First = const_iterator(_Lonode, this);
const_iterator _Last = const_iterator(_Hinode, this);
return (_Paircc(_First, _Last));
}
如果您只想查找键是否存在,并且不关心值,最好使用 map::count
,因为它只返回一个整数。 map::find
返回一个迭代器,因此通过使用 count
,您将节省迭代器的构造。
相关文章:
- 在 Windows 上,是否可以让 dll 在不使用 PATH 环境变量的情况下在另一个文件夹中查找依赖项?
- 使用XOR查找O(n)-解决方案中的两个字符串是否为变位符
- 查找数字是否为 2 的幂的时间复杂度
- 由并发无序映射查找线程调用的函数是否安全?
- 在迷宫c++中查找路径是否存在
- 检查IP是否在其他IP网络范围内,并查找下一个可用IP
- 查找数组中是否存在C++元素
- 查找函数是否为常量
- 拆分字符串输入并查找字符是否有效
- 是否有用于排序和查找 c++ 中是否已存在的值的函数?
- 查找字符串是否包含字符串向量的任何一个元素的最佳方法
- 如何使用C或C 查找给定的IPv6地址是否属于CIDR范围
- std::unordered_set 是否对任何对象进行摊销常量查找时间,而不考虑谓词
- 我在代码中收到一个运行时错误,该错误如何通过修改最多一个元素来查找数组是否可以变得不减少
- 这个查找查询是否出错,因为我输入的数字太大
- 查找数字是否为素数 c++
- 如何使用WinINet查找是否存在网页
- 名称查找是否应该"in the class of the object expression"查找该类的成员?
- c 预处理器 - C++ #include查找是否有任何标准要求
- 两阶段查找:是否可以轻松地混合继承和模板