Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 空列表迭代器比较始终为true_C++_List_Iterator - Fatal编程技术网

C++ 空列表迭代器比较始终为true

C++ 空列表迭代器比较始终为true,c++,list,iterator,C++,List,Iterator,我正在使用以下代码测试空列表的列表迭代器: 代码 #include <iostream> #include <list> int main(){ std::list<int> l; bool a, b, c; std::list<int>::iterator i = l.begin(); a = i == --l.end(); b = ++i == l.end(); c = ++i == l.en

我正在使用以下代码测试空列表的列表迭代器:

代码

#include <iostream>
#include <list>


int main(){
    std::list<int> l;
    bool a, b, c;
    std::list<int>::iterator i = l.begin();
    a = i == --l.end();
    b = ++i == l.end();
    c = ++i == l.end();
    std::cout << a << std::endl;
    std::cout << b << std::endl;
    std::cout << c << std::endl;
}

三个布尔值的结果总是正确的,但是我在增加和减少迭代器,为什么它们总是指向同一个地址?这只是未定义的行为,因为不允许以使迭代器超出基础范围的方式增加或减少迭代器

在本例中,由于范围为空,因此所有三个操作都是非法和不正确的。

--l.end()是带有空容器的UB。
1
1
1