我刚刚写了一个嵌套在另一个for循环中的for循环,都使用int i作为增量,看起来像这样:
for (int i = 0; i < 10; ++i) {
std::cout << i << ": ";
for (int i = 0; i < 10; ++i) {
std::cout << i << ", ";
}
std::cout << "\n";
}
使用MSVC编译时,会产生以下结果:
0: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
1: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
2: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
3: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
4: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
5: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
6: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
7: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
8: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
9: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
使用不同的编译器时,此代码的行为是否相同?这是未定义的行为吗?