![0xFFFF 0xFFFF]()
本文介绍了使用ushort索引循环使用ushort端口的所有值....谨慎。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 为所有值编写循环的最好方法是什么? unsigned short(0..0xffff),usnig''unsigned short port;''作为索引? 为了比较,只有一种方法可以写入for(k = 0; k +)循环,但似乎有几种微妙的方法来枚举所有 0..0xffff值使用ushort。 不,for(port = 0; port< PORT_MAX; port ++){}不是其中之一他们。 雅科夫What would be the nicest way to write the loop for all values ofunsigned 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 all0..0xffff values using ushort.And no, for(port=0; port <PORT_MAX; port++) {} is not one of them.Yakov推荐答案 显然不是因为换行。 最好的是在旁观者的眼中。就个人而言,我会使用未签名的 long int作为索引,但是你说你不想要那个。 这里''单程: int flag = 0; for(port = 0; flag == 0; port ++) { do_stuff(port); if(port == PORT_MAX) { flag = 1; } } 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 unsignedlong 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 显然不是因为换行。 最好的是在旁观者的眼中。就个人而言,我会使用未签名的 long int作为索引,但是你说你不想要那个。 这里''单程: int flag = 0; for(port = 0; flag == 0; port ++) { do_stuff(port); if(port == PORT_MAX) { flag = 1; } } 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 unsignedlong 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 thatlong is strictly wider that short.That would be weird platform, you''d say.Yes, that would be weird platform.So what.Still adhering to thestandard.But I''d not want the code to be broken by such very subtle assumtions. 指数比所讨论的类型更广泛的用户。 雅科夫index of wider int than the type in question, the ushort.Yakov { unsigned short port = 0; do { / * **带端口的东西 * / }而(++端口) != 0); }{unsigned short port = 0;do{/*** something with port*/} while (++port != 0);} 这篇关于使用ushort索引循环使用ushort端口的所有值....谨慎。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-23 06:28