当std::set<>::iterator
未初始化时,它不等于集合中的任何其他迭代器,但它等于其他未初始化的迭代器。
这是特定于 GCC 的实现吗?(未初始化的迭代器实际上是否初始化为无效值?)
#include <stdio.h>
#include <iostream>
#include <set>
#include <vector>
int main()
{
std::set<int> s;
std::set<int>::reverse_iterator inv = s.rend();
std::cout << (inv == s.rend()) << "\n";
std::cout << (inv == s.rbegin()) << "\n";
s.insert(5);
std::cout << (inv == s.rend()) << "\n";
std::cout << (inv == s.rbegin()) << "\n";
// invalidate
inv = std::set<int>::reverse_iterator();
std::cout << (inv == s.rend()) << "\n";
std::cout << (inv == s.rbegin()) << "\n";
auto inv2 = std::set<int>::reverse_iterator();
std::cout << (inv == inv2) << "!!!\n";
return 0;
}
印刷:
1
1
0
1
0
0
1!!!