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.

有帮助吗?

解决方案

for( int x=0 ; ; x = (x+1) % 4 ) {
  // body of loop
}

其他提示

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
   }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top