for( int x=0 ; ; x = (x+1) % 4 ) {
// body of loop
}
Circular iteration in C++
-
29-08-2022 - |
题
I am looking for a simple way to iterate through a set of integers in C++. For example, if I had an integer variable 'x' and wanted to use the increment statement 'x++' continuously over 4 integer values, the desired output would be something like '0 1 2 3 0 1 2...'.
I know that a circularly linked list is a solution, but it just seems like overkill to me, I really need something short and sweet. I suspect that enumerated types may be able to do something like this, but my research has not turned up anything.
解决方案
其他提示
while (1) {
for ( int x = 0; x < 4; x++ ) {
//...
}
}
Try this:
for (int i = 0; i < n; i = i+4) {
for (int j = 0; j < i; j++) {
// print here
}
}
不隶属于 StackOverflow