关于“环”的

来源:互联网 发布:plc编程入门梯形图视频 编辑:程序博客网 时间:2024/06/03 06:49

假如:int arr[10];   int index; 如果想将此数组表示成“环”。往后可以这么写:index = (index == 9) ? 0 : (index + 1);   当然,往前这么表示:index = (index == 0) ? 9 : (index - 1);

 还有个更高级的:往后:index = (index + 1) % 10;    往前:index = (index - 1 + 10) % 10;    <=  套路,套路......

 

原创粉丝点击