当我应该使用std :: map :: at检索地图元素时 [英] When I should use std::map::at to retrieve map element
问题描述
我在堆栈溢出,但对我来说,尚不清楚是否有任何例外情况是最好使用 std :: map :: at
检索地图元素。
根据定义, std :: map :: at
返回对用
键k标识的元素的映射值的引用。
如果k与任何键的键都不匹配容器中的元素,
函数将引发out_of_range异常。
对我来说,只有在值得使用 std :: map :: at
,当您100%确定具有特定键的元素存在时,否则应考虑进行异常处理。
- 是否存在任何ca哪里
std :: map :: at
被认为是最有效,最优雅的方法?在哪种情况下,您将建议使用std :: map :: at
吗? - 我对,最好使用
map :: find()
是否有可能没有这样的键?而map :: find()
是更快,更优雅的方法吗?
if(map.find( key)!= map.end())
{
//找到
} else
{
//未找到
}
ps
map :: operator []
有时可能很危险,因为如果一个元素不存在,它将被插入。
已编辑:链接以某种方式相关链接1 链接2 链接3 链接4 链接5 链接6
与大多数现有答案相反,请注意,实际上有 4 个方法与在地图中查找元素有关(忽略 lower_bound
, upper_bound
和 equal_range
,它们不太精确):
-
operator []
仅存在于非const版本中,如前所述,如果不存在,它将创建元素 -
at()
,如果存在该元素,则返回对该元素的引用,否则抛出异常 -
find()
返回元素的迭代器(如果存在),或者返回map :: end()
的迭代器不 -
count()
在映射中$ c $返回此类元素的数量c>,这个是0或1
现在语义很清楚,让我们回顾一下何时使用:
- 如果您只想知道
映射
中是否存在某个元素(或不存在),请使用count()
。 - 如果您希望访问元素,则该元素应位于
映射中
,然后使用at()
。 - 如果您要访问元素,并且不知道它是否是是否在
地图
中,然后使用find()
;不要忘记检查生成的迭代器是否等于end()
的结果。 - 最后,如果您希望访问元素(如果存在)或创建(并访问)(如果不存在),请使用
operator []
;如果您不想调用类型默认构造函数来创建它,则可以适当地使用insert
或emplace
I have read different articles on web and questions at stackoverflow, but for me it is not clear is there any exclusive case when it is better to use std::map::at
to retrieve map element.
According to definition, std::map::at
Returns a reference to the mapped value of the element identified with key k.
If k does not match the key of any element in the container, the function throws an out_of_range exception.
For me only case when it is worth to use std::map::at
when you 100% sure that element with particular key exist, otherwise you should consider exception handling.
- Is there any case where
std::map::at
considered as most efficient and elegant way to do? In what cases you will recommend to usestd::map::at
? - Am I right that it is better to use
map::find()
when there is a possibility to not have element with such a key? Andmap::find()
it is faster and more elegant approach?
if ( map.find("key") != map.end() ) { // found } else { // not found }
p.s
map::operator[]
sometimes can be dangerous, because if an element doesn't exist then it will inserts it.
EDITED: links somehow related link 1 link 2 link 3 link 4 link 5 link 6
Contrary to most existing answers here, note that there are actually 4 methods related to finding an element in a map (ignoring lower_bound
, upper_bound
and equal_range
, which are less precise):
operator[]
only exist in non-const version, as noted it will create the element if it does not existat()
, introduced in C++11, returns a reference to the element if it exists and throws an exception otherwisefind()
returns an iterator to the element if it exists or an iterator tomap::end()
if it does notcount()
returns the number of such elements, in amap
, this is 0 or 1
Now that the semantics are clear, let us review when to use which:
- if you only wish to know whether an element is present in the
map
(or not), then usecount()
. - if you wish to access the element, and it shall be in the
map
, then useat()
. - if you wish to access the element, and do not know whether it is in the
map
or not, then usefind()
; do not forget to check that the resulting iterator is not equal to the result ofend()
. - finally, if you wish to access the element if it exists or create it (and access it) if it does not, use
operator[]
; if you do not wish to call the type default constructor to create it, then use eitherinsert
oremplace
appropriately
这篇关于当我应该使用std :: map :: at检索地图元素时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!