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