使用ushort索引循环使用ushort端口的所有值....谨慎。 [英] loop for all values of ushort ports using ushort index .... with caution.

查看:89
本文介绍了使用ushort索引循环使用ushort端口的所有值....谨慎。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为所有值编写循环的最好方法是什么?
unsigned short(0..0xffff),usnig''unsigned short port;''作为索引?

为了比较,只有一种方法可以写入for(k = 0; k <1000; k +

+)循环,但似乎有几种微妙的方法来枚举所有

0..0xffff值使用ushort。


不,for(port = 0; port< PORT_MAX; port ++){}不是其中之一他们。


雅科夫

What would be the nicest way to write the loop for all values of
unsigned short (0..0xffff), usnig ''unsigned short port;'' as an index ?
For comparison, there is just one way to write the for(k=0; k<1000; k+
+) loop, but there seems to be several subtle ways to enumerate all
0..0xffff values using ushort.

And no, for(port=0; port <PORT_MAX; port++) {} is not one of them.

Yakov

推荐答案

雅科夫说:
Yakov said:

为所有值写的循环最好的方法

unsigned short(0..0xffff),usnig''unsigned short port;''as一个索引?

为了比较,只有一种方法来编写for(k = 0; k <1000; k +

+)循环,但似乎有使用ushort枚举所有

0..0xffff值的几种微妙方法。


和no,for(port = 0; port< PORT_MAX; port ++){}不是其中之一。
What would be the nicest way to write the loop for all values of
unsigned short (0..0xffff), usnig ''unsigned short port;'' as an index ?
For comparison, there is just one way to write the for(k=0; k<1000; k+
+) loop, but there seems to be several subtle ways to enumerate all
0..0xffff values using ushort.

And no, for(port=0; port <PORT_MAX; port++) {} is not one of them.



显然不是因为换行。


最好的是在旁观者的眼中。就个人而言,我会使用未签名的

long int作为索引,但是你说你不想要那个。


这里''单程:


int flag = 0;

for(port = 0; flag == 0; port ++)

{

do_stuff(port);

if(port == PORT_MAX)

{

flag = 1; < br $>
}

}


Blech,对吗?


这是'另一个方式:


port = 0;

top:

{

do_stuff(port);

if(port< PORT_MAX)

{

++ port;

goto top; / *咳嗽,喋喋不休,咕噜咕噜,aaargh * /

}

}


-

Richard Heathfield< http://www.cpax.org.uk>

电子邮件:-www。 + rjh @

谷歌用户:< http://www.cpax.org.uk/prg/writings/googly.php>

Usenet是一个奇怪的放置" - dmr 1999年7月29日

Obviously not, because of wrap.

"Nicest" is in the eye of the beholder. Personally, I''d use an unsigned
long int as the index, but you say you don''t want that.

Here''s one way:

int flag = 0;
for(port = 0; flag == 0; port++)
{
do_stuff(port);
if(port == PORT_MAX)
{
flag = 1;
}
}

Blech, right?

Here''s another way:

port = 0;
top:
{
do_stuff(port);
if(port < PORT_MAX)
{
++port;
goto top; /* cough, splutter, gurgle, aaargh */
}
}

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999


6月26日凌晨1点02分,Richard Heathfield< r ... @ see.sig.invalidwrote:
On Jun 26, 1:02 am, Richard Heathfield <r...@see.sig.invalidwrote:

雅科夫说:
Yakov said:

为所有值编写循环最好的方法

unsigned short(0..0xffff),usnig''unsigned short port;''作为索引?

为了比较,只有一种方法可以写入for(k = 0; k< 1000; k +

+)循环,但似乎有几种微妙的方法可以使用ushort枚举所有

0..0xffff值。
What would be the nicest way to write the loop for all values of
unsigned short (0..0xffff), usnig ''unsigned short port;'' as an index ?
For comparison, there is just one way to write the for(k=0; k<1000; k+
+) loop, but there seems to be several subtle ways to enumerate all
0..0xffff values using ushort.


不,for(port = 0; port< PORT_MAX; port ++){}不是其中之一。
And no, for(port=0; port <PORT_MAX; port++) {} is not one of them.



显然不是因为换行。


最好的是在旁观者的眼中。就个人而言,我会使用未签名的

long int作为索引,但是你说你不想要那个。


这里''单程:


int flag = 0;

for(port = 0; flag == 0; port ++)

{

do_stuff(port);

if(port == PORT_MAX)

{

flag = 1; < br $>
}


}


Blech,对吧?


这里另一种方式:


port = 0;

top:

{

do_stuff (港口);

if(port< PORT_MAX)

{

++ port;

转到顶部; / *咳嗽,喋喋不休,咕噜咕噜,aaargh * /

}


}


Obviously not, because of wrap.

"Nicest" is in the eye of the beholder. Personally, I''d use an unsigned
long int as the index, but you say you don''t want that.

Here''s one way:

int flag = 0;
for(port = 0; flag == 0; port++)
{
do_stuff(port);
if(port == PORT_MAX)
{
flag = 1;
}

}

Blech, right?

Here''s another way:

port = 0;
top:
{
do_stuff(port);
if(port < PORT_MAX)
{
++port;
goto top; /* cough, splutter, gurgle, aaargh */
}

}



I相信标准不包含在任何地方,保证

long是非常宽的那么短。


这将是一个奇怪的平台,你会说。

是的,这将是一个奇怪的平台。所以什么。仍然坚持

标准。


但我''我不希望代码被这种非常微妙的假设打破。

I believe the standard does not contain, anywhere, the guarantee that
long is strictly wider that short.

That would be weird platform, you''d say.
Yes, that would be weird platform.So what.Still adhering to the
standard.

But I''d not want the code to be broken by such very subtle assumtions.


>从这里开始,不依赖假设你可以拥有
>From here, the desire to not rely on assumptions that you can have



指数比所讨论的类型更广泛的用户。


雅科夫

index of wider int than the type in question, the ushort.

Yakov


6月26日,7:50 am,Yakov< iler ... @ gmail.comwrote:
On Jun 26, 7:50 am, Yakov <iler...@gmail.comwrote:

为所有值
$ b编写循环最好的方法是什么$ b unsigned short(0..0xffff),usnig''unsigned short port;''作为索引?

为了比较,只有一种方法可以写入for(k = 0; k< ; 1000; k +

+)循环,但似乎有几种微妙的方法可以使用ushort枚举所有

0..0xffff值。

不,for(port = 0; port< PORT_MAX; port ++){}不是其中之一。
What would be the nicest way to write the loop for all values of
unsigned short (0..0xffff), usnig ''unsigned short port;'' as an index ?
For comparison, there is just one way to write the for(k=0; k<1000; k+
+) loop, but there seems to be several subtle ways to enumerate all
0..0xffff values using ushort.

And no, for(port=0; port <PORT_MAX; port++) {} is not one of them.



{

unsigned short port = 0;


do

{

/ *

**带端口的东西

* /

}而(++端口) != 0);

}

{
unsigned short port = 0;

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


这篇关于使用ushort索引循环使用ushort端口的所有值....谨慎。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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