m.find(...) == m.end() - 使用的是迭代器还是 const_iterator

发布于 2024-08-28 13:34:22 字数 274 浏览 3 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不可一世的女人 2024-09-04 13:34:22

如果mconst,则返回一个const_iterator;否则将返回一个迭代器

如果您所做的只是测试地图中是否存在某个元素,那么使用哪个元素并不重要。

If m is const, then a const_iterator will be returned; otherwise an iterator 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.

无戏配角 2024-09-04 13:34:22

这取决于你的地图是否是常量。如果是,你将得到一个 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文