【问题标题】:Does std::next check if we are already at the end of the container? [duplicate]std::next 是否检查我们是否已经在容器的末尾? [复制]
【发布时间】:2017-08-17 00:13:28
【问题描述】:

假设我有一个容器的迭代器std::list::iterator it = mylist.begin(),我调用了

iterator it2 = std::next(it,1);

std::next 是否检查it == mylist.end()

【问题讨论】:

  • 你的意思是说!= mylist.end() 。不,它不检查
  • 如果指定的递增或递减序列需要递增不可递增的迭代器(例如过去的迭代器),或者不可递减的迭代器 (如前迭代器或单数迭代器)递减。

标签: c++ stl containers


【解决方案1】:

不,程序在那种情况下被阻塞了。你应该检查自己:

if (it != mylist.end())
    it2 = std::next(it,1);

【讨论】:

  • 你知道答案,那么为什么要问这个问题?
  • @HariomSingh SO 允许并鼓励self answered questions
  • @Ron 虽然问题已经被提出和回答时并没有那么多
  • @M.M 我同意这一点。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-08
  • 2012-05-13
  • 1970-01-01
相关资源
最近更新 更多