假定数组

 

int a[n];

int index 表示当前的索引

要求显示输出当前index,并自加,如果index大于数组长度,则index=0

/*常规写法*/

if (index >= n)
   index = 0

cout<<a[index++]<<endl;
 
/*推荐的写法*/

index = ++index%n;
cout<<a[index]<<endl;

 

posted on 2016-01-10 11:48  zooz  阅读(251)  评论(0编辑  收藏  举报