c++ - 空迭代器会导致未定义的行为吗?

标签 c++ iterator

using namespace std;
class myList
{
  public:
    mylist():_internalList(),_lastPostition(0)
  {
  }
    typedef list<string>::iterator Itr;
    bool enqueue(string);
    Itr next()
    {
      if(_lastPostition == 0)
        _lastPostition = _internalList.begin();
      if(_lastPostition == _internalList.end())
        return (_lastPostition = 0);
      return _lastPostition++;
    }
  private:
    list<string> _internalList;
    Itr _lastPostition;
}

enqueue不是 push_back ,它根据一些自定义逻辑插入。 我不能使用 std::set和重载operator < ,因为我的插入逻辑不是可传递的 - ( a < b && b < c)并不意味着 a < c .

这有效,但我不确定它是否是未定义的行为。 将 0 分配给迭代器并检查 0 是否安全?

最佳答案

“将 0 分配给迭代器并检查 0 是否安全?”没有。

关于c++ - 空迭代器会导致未定义的行为吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5374979/

相关文章:

c++ - Matlab mex 选项未更新

c++ - shared_ptr 构造函数参数是否应该按值传递

c++ - 输入迭代器可以重复读取而输出迭代器只能写入一次?

链表的C++继承

c++ - 我想将我的 arr[] 值存储为 2 的幂。但是我得到的一些输出并不是我真正想要的

c++ - 什么是 C++ 序言,以及 get 函数的前置后置条件

javascript - 在 for...in 循环中重置迭代器

c++ - 将指针转换为 STL 中的反向 vector 迭代器

java - 为什么当我尝试删除元素时此 ArrayList 会抛出 ConcurrentModificationException?

iterator - 从 Julia 中的向量生成所有无序对