美好的一天,同事们!
我需要获取从0到255的连续数字的循环序列.使用无符号字符溢出是否合法:
unsigned char test_char = 0;
while (true) {
std::cout << test_char++ << " ";
}
Run Code Online (Sandbox Code Playgroud)
或者更安全地使用此代码:
int test_int = 0;
while (true) {
std::cout << test_int++ % 256 << " ";
}
Run Code Online (Sandbox Code Playgroud)
当然,在实际代码中会有合理的条件而不是while(true).