gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 21:07:20 24 4
gpt4 key购买 nike

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com