为什么 `std::set<T>::end()` 不等于 `std::set<T>::iterator{}`?
Why does not `std::set<T>::end()` compare equal to `std::set<T>::iterator{}`?
编译这段代码:
#include <set>
#include <iostream>
int main(int argc, char * argv[]){
std::set<int> test;
std::cout << (test.end() == std::set<int>::iterator{}) << std::endl;
std::cout << (test.begin() == std::set<int>::iterator{}) << std::endl;
std::cout << (test.begin() == test.end()) << std::endl;
return 0;
}
它输出(在 gcc 11.1 和 clang 12.0.0 上测试):
0
0
1
...但是,如果我指的是https://timsong-cpp.github.io/cppwp/n4659/iterators#forward.iterators-2
The domain of == for forward iterators is that of iterators over the same underlying sequence. However, value-initialized iterators may be compared and shall compare equal to other value-initialized iterators of the same type. [ Note: Value-initialized iterators behave as if they refer past the end of the same empty sequence. — end note ]
...然后 std::set<int>::iterator{}
是我所理解的“值初始化”(请注意,我使用临时变量得到相同的结果),我希望输出为:
1
1
1
我错过了什么?
将值初始化的迭代器与序列中的迭代器进行比较是未定义的行为。
“值初始化迭代器的行为就好像它们引用了相同空序列的末尾一样”表示它们不能被取消引用或推进。
The domain of == for forward iterators is that of iterators over the same underlying sequence.
所以...可以比较来自相同序列的迭代器与 ==
。
However, value-initialized iterators may be compared and shall compare equal to other value-initialized iterators of the same type.
所以...可以比较两个相同类型的值初始化迭代器==
。
Note: Value-initialized iterators behave as if they refer past the end of the same empty sequence.
注释不规范,但这是在描述隐藏了一些空序列,并且值初始化的迭代器就好像它们引用了这个序列的结束迭代器一样。
这就是说,就好像标准库为自己实例化了一个 std::set<int>
和一个值初始化的迭代器引用了它的 .end()
。
注释与上述一致。如果值初始化迭代器的行为就好像它们都指向同一个序列,在同一个点,它们将是可比较的并且比较相等。
编译这段代码:
#include <set>
#include <iostream>
int main(int argc, char * argv[]){
std::set<int> test;
std::cout << (test.end() == std::set<int>::iterator{}) << std::endl;
std::cout << (test.begin() == std::set<int>::iterator{}) << std::endl;
std::cout << (test.begin() == test.end()) << std::endl;
return 0;
}
它输出(在 gcc 11.1 和 clang 12.0.0 上测试):
0
0
1
...但是,如果我指的是https://timsong-cpp.github.io/cppwp/n4659/iterators#forward.iterators-2
The domain of == for forward iterators is that of iterators over the same underlying sequence. However, value-initialized iterators may be compared and shall compare equal to other value-initialized iterators of the same type. [ Note: Value-initialized iterators behave as if they refer past the end of the same empty sequence. — end note ]
...然后 std::set<int>::iterator{}
是我所理解的“值初始化”(请注意,我使用临时变量得到相同的结果),我希望输出为:
1
1
1
我错过了什么?
将值初始化的迭代器与序列中的迭代器进行比较是未定义的行为。
“值初始化迭代器的行为就好像它们引用了相同空序列的末尾一样”表示它们不能被取消引用或推进。
The domain of == for forward iterators is that of iterators over the same underlying sequence.
所以...可以比较来自相同序列的迭代器与 ==
。
However, value-initialized iterators may be compared and shall compare equal to other value-initialized iterators of the same type.
所以...可以比较两个相同类型的值初始化迭代器==
。
Note: Value-initialized iterators behave as if they refer past the end of the same empty sequence.
注释不规范,但这是在描述隐藏了一些空序列,并且值初始化的迭代器就好像它们引用了这个序列的结束迭代器一样。
这就是说,就好像标准库为自己实例化了一个 std::set<int>
和一个值初始化的迭代器引用了它的 .end()
。
注释与上述一致。如果值初始化迭代器的行为就好像它们都指向同一个序列,在同一个点,它们将是可比较的并且比较相等。