改变0到1之间的索引 [英] change index between 0 and 1

查看:56
本文介绍了改变0到1之间的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我有一个函数有一个静态数组,其中有两个元素在

中。当第一次调用该函数时,它应该访问

元素0,第二次它应该访问元素1,第三次

元素0,第四个元素1我可以通过保持索引的静态

变量和if / else-statement来实现它但是有一个不需要的更聪明的方式if / else?

Hello, I have a function that has a static array with two elements in
it. When the function is called the first time, it should access
element 0, the second time it should access element 1, the third time
element 0, the fourth element 1 etc. I can do it by having a static
variable holding the index and and if/else-statement but is there a
more clever way that doesn''t need the if/else?

推荐答案

" Eric Lilja" < MI ******** @ gmail.com>写道:
"Eric Lilja" <mi********@gmail.com> writes:
你好,我有一个函数,它有一个带有两个元素的静态数组。当第一次调用该函数时,它应该访问元素0,第二次访问元素1,第三次元素0,第四元素1等我可以通过拥有一个静态的变量保存索引和if / else-statement但是有一个更聪明的方法,不需要if / else吗?
Hello, I have a function that has a static array with two elements in
it. When the function is called the first time, it should access
element 0, the second time it should access element 1, the third time
element 0, the fourth element 1 etc. I can do it by having a static
variable holding the index and and if/else-statement but is there a
more clever way that doesn''t need the if/else?



显而易见的方法是将索引作为参数传递,

但我认为你可以独立于调用者这样做。


这里有一种方法:


void func(无论如何)

{

static int index = 0;

static element_type arr [2];

/ * ...引用arr [index]的代码... * /

index = 1 - 索引;

}


对于2以外的大小,类似这样的东西:


index =(指数+ 1)%ARRAY_SIZE;


更为一般。


-

Keith Thompson(The_Other_Keith) ks *** @ mib.org < http://www.ghoti.net/~kst>

圣地亚哥超级计算机中心< *> < http://users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。



The obvious way to do this would be to pass the index as a parameter,
but I presume you to do this independently of the caller.

Here''s one approach:

void func(whatever)
{
static int index = 0;
static element_type arr[2];
/* ... code referring to arr[index] ... */
index = 1 - index;
}

For sizes other than 2, something like this:

index = (index + 1) % ARRAY_SIZE;

is more general.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.


Eric Lilja写道:
Eric Lilja wrote:
你好,我有一个函数,它有一个带有两个元素的静态数组<它。当第一次调用该函数时,它应该访问元素0,第二次访问元素1,第三次元素0,第四元素1等我可以通过拥有一个静态的变量保存索引和if / else-statement但是有一个更聪明的方法,不需要if / else吗?
Hello, I have a function that has a static array with two elements in
it. When the function is called the first time, it should access
element 0, the second time it should access element 1, the third time
element 0, the fourth element 1 etc. I can do it by having a static
variable holding the index and and if/else-statement but is there a
more clever way that doesn''t need the if/else?



static unsigned index = 1;


返回数组[index ^ = 1];


-

Ian Collins。


static unsigned index = 1;

return array[index^=1];

--
Ian Collins.


Eric Lilja写道:
Eric Lilja wrote:

你好,我有一个函数,它有一个带有两个元素的静态数组<它。当第一次调用该函数时,它应该访问元素0,第二次访问元素1,第三次元素0,第四元素1等我可以通过拥有一个静态的变量保存索引和if / else-statement但是有一个更聪明的方法,不需要if / else吗?

Hello, I have a function that has a static array with two elements in
it. When the function is called the first time, it should access
element 0, the second time it should access element 1, the third time
element 0, the fourth element 1 etc. I can do it by having a static
variable holding the index and and if/else-statement but is there a
more clever way that doesn''t need the if/else?



/ * BEGIN new.c * /


#include< stdio.h>


void sequence(size_t first ,size_t秒);


int main(无效)

{

无符号翻转;

size_t index;


puts(" / * BEGIN new.c output * / \ n");

for(index = 0; index! = 5; ++ index){

sequence(flip ++& 1,index);

sequence(flip ++& 1,index);

}

puts(" \\\
\\\
/ * END new.c output * /");

返回0;

}


void sequence(size_t first,size_t second)

{

int array [] [5] = {0,2,4,6,8,1,3,5,7,9};


putchar(''0''+ array [first] [second]);

}


/ * END new.c * /


-

pete



/* BEGIN new.c */

#include <stdio.h>

void sequence(size_t first, size_t second);

int main(void)
{
unsigned flip;
size_t index;

puts("/* BEGIN new.c output */\n");
for (index = 0; index != 5; ++index) {
sequence(flip++ & 1, index);
sequence(flip++ & 1, index);
}
puts("\n\n/* END new.c output */");
return 0;
}

void sequence(size_t first, size_t second)
{
int array[][5] = {0,2,4,6,8,1,3,5,7,9};

putchar(''0'' + array[first][second]);
}

/* END new.c */

--
pete


这篇关于改变0到1之间的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆