for循环遍历变量的整个范围 [英] A for loop iterating over the complete range of a variable

查看:111
本文介绍了for循环遍历变量的整个范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

阅读Skybuck Flying的明显的巨魔消息,我想到了如何使用
正确地执行一个for循环,这将循环遍及整个范围

一个无符号变量。众所周知,Skybuck的方法不起作用。

还有很多其他方法。您可以使用更宽的类型进行

计数,但那会是作弊。您还可以在循环外处理

第一次或最后一次迭代,但是它接下来它将不仅仅是一个循环。你可以制作一个非常大的整数

标志来判断你是否已经访问了一个值,但这会消耗太多内存。

会占用太多内存。

所以我安顿下来这个版本。假设unsigned short是16位

宽。


int stop = 0;

unsigned short i;

for(i = 0;!stop; stop = ++ i == 0){

/ *做点什么* /

}


还有更优雅的解决方案吗?


-

/ - Joona Palaste(pa ***** @ cc .helsinki.fi)-------------芬兰-------- \

\-- http://www.helsinki.fi/~palaste -------------- -------规则! -------- /

冰淇淋销售不知何故导致溺水:两者都发生在夏季。

- Antti Voipio& Arto Wikla

Reading Skybuck Flying''s obvious troll message, I thought of how to
properly do a for loop that would iterate over the complete range of
an unsigned variable. As you all know, Skybuck''s method won''t work.
There are a number of other ways. You could use a wider type to do the
counting, but that would be cheating. You could also handle either the
first or last iteration as a special case outside the loop, but then it
would be more than a loop. You could make a very big array of integer
flags to tell whether you''ve already visited a value, but that would
consume too much memory.
So I settled down to this version. Assume unsigned short is 16 bits
wide.

int stop = 0;
unsigned short i;
for (i=0; !stop; stop = ++i==0) {
/* do something */
}

Any more elegant solutions out there?

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"Ice cream sales somehow cause drownings: both happen in summer."
- Antti Voipio & Arto Wikla

推荐答案

Joona I Palaste< pa ***** @ cc.helsinki.fi>潦草地写道:
Joona I Palaste <pa*****@cc.helsinki.fi> scribbled the following:
所以我选择了这个版本。假设无符号短路为16位宽。


其实你不需要假设...

int stop = 0;
unsigned short i;
for(i = 0;!stop; stop = ++ i == 0){
/ *做点什么* /
}
还有更优雅的解决方案吗?
So I settled down to this version. Assume unsigned short is 16 bits
wide.
Actually you don''t need to assume that...
int stop = 0;
unsigned short i;
for (i=0; !stop; stop = ++i==0) {
/* do something */
} Any more elegant solutions out there?




-

/ - Joona Palaste(pa*****@cc.helsinki.fi)---------- ---芬兰-------- \

\-- http://www.helsinki.fi/~palaste ---------------------规则! -------- /

我希望我们认识的人会死,所以我们可以给他们留下鲜花。

- 一个6岁的孩子女孩,在墓地里看到鲜花



--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"I wish someone we knew would die so we could leave them flowers."
- A 6-year-old girl, upon seeing flowers in a cemetery




2004年8月29日,Sun,Joona I Palaste写道:

On Sun, 29 Aug 2004, Joona I Palaste wrote:

阅读Skybuck Flying的明显巨魔消息,我想到了如何正确地执行一个for循环,它会遍历整个范围的无符号变量。众所周知,Skybuck的方法不起作用。
还有很多其他方法。您可以使用更宽的类型来进行计数,但那会是作弊。您还可以将第一次或最后一次迭代作为循环外的特殊情况处理,但它不仅仅是一个循环。你可以制作一个非常大的整数
标志来判断你是否已经访问了一个值,但这会消耗太多的内存。
所以我选择了这个版本。假设unsigned short是16位宽。

int stop = 0;
unsigned short i;
for(i = 0;!stop; stop = ++ i == 0){
/ *做点什么* /
}

有更优雅的解决方案吗?

Reading Skybuck Flying''s obvious troll message, I thought of how to
properly do a for loop that would iterate over the complete range of
an unsigned variable. As you all know, Skybuck''s method won''t work.
There are a number of other ways. You could use a wider type to do the
counting, but that would be cheating. You could also handle either the
first or last iteration as a special case outside the loop, but then it
would be more than a loop. You could make a very big array of integer
flags to tell whether you''ve already visited a value, but that would
consume too much memory.
So I settled down to this version. Assume unsigned short is 16 bits
wide.

int stop = 0;
unsigned short i;
for (i=0; !stop; stop = ++i==0) {
/* do something */
}

Any more elegant solutions out there?




我相信你已经看到每次有人发布的解决方案

声称它很难做到:


unsigned短i = 0;

做{

/ *做点什么* /

}而(++ i!= 0);


这不是''for'循环,但它很有效,很容易记住和写。

我想你可以使用''对于''类似于你的循环,但它仍然会更慢并使用一个临时变量:[未测试的代码]


unsigned short i;

int tmp;

for(tmp = 0,i = 0; i ||!tmp; ++ i,tmp = 1){

/ *做点什么* /

}


-Arthur



I''m sure you''ve seen the solution that gets posted every time someone
claims it''s hard to do:

unsigned short i = 0;
do {
/* do something */
} while (++i != 0);

It''s not a ''for'' loop, but it works and is easy to remember and write.
I guess you could use a ''for'' loop similar to yours, but it would still
be slower and use a temporary variable: [UNTESTED CODE]

unsigned short i;
int tmp;
for (tmp=0, i=0; i || !tmp; ++i, tmp=1) {
/* do something */
}

-Arthur


文章< CG ********** @ö ravannahka.helsinki.fi>,

Joona I Palaste< pa ***** @ cc.helsinki.fi>写道:
In article <cg**********@oravannahka.helsinki.fi>,
Joona I Palaste <pa*****@cc.helsinki.fi> wrote:
int stop = 0;
unsigned short i;
for(i = 0;!stop; stop = ++ i == 0){
/ *做点什么* /
}
int stop = 0;
unsigned short i;
for (i=0; !stop; stop = ++i==0) {
/* do something */
}




如果你使用for循环,你一定需要一些额外的变量,因为

开始时的测试必须成功65536次并且失败一次,所以你需要
需要65537个州。


你可以避免通过使用do ... while

循环来使用额外的变量,最后测试结果因此只需要成功65535

次:


i = 0;

做{

/ *做点什么* /

i ++;

} while(i!= 0);


- Richard



If you use a for loop, you are bound to need some extra variable, since
the test at the start has to succeed 65536 times and fail once, so you
need 65537 states.

You can avoid the use of an extra variable by using a do ... while
loop, which tests at the end and therefore needs to succeed only 65535
times:

i = 0;
do {
/* do something */
i++;
} while(i != 0);

-- Richard


这篇关于for循环遍历变量的整个范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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