m.find(...) == m.end() - 使用的是迭代器还是 const_iterator
std::map find/end 都提供 const_iterator 和迭代器,例如
iterator end ();
const_iterator end () const
出于好奇,如果我有一个 std::map ,它将在这里被调用/比较,是迭代器还是 const_iterator ? :
if(m.find(key) != m.end()) {
...
}
我应该关心吗?
std::map find/end both provides const_iterator and iterator, e.g.
iterator end ();
const_iterator end () const
Out of curiosity,if I have a std::map , which will be called/compared here, an iterator or a const_iterator ? :
if(m.find(key) != m.end()) {
...
}
And should I care ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果
m
是const
,则返回一个const_iterator
;否则将返回一个迭代器
。如果您所做的只是测试地图中是否存在某个元素,那么使用哪个元素并不重要。
If
m
isconst
, then aconst_iterator
will be returned; otherwise aniterator
will be returned.If all you are doing is testing for existence of an element in the map, then it doesn't really matter which one is used.
这取决于你的地图是否是常量。如果是,你将得到一个 const_iterator。如果没有,你会得到一个迭代器。
It depends on if your map is const or not. If it is, you'll get a const_iterator. If not, you get an iterator.