使用无符号整数类型进行迭代 [英] Iteration with unsigned integral type

查看:91
本文介绍了使用无符号整数类型进行迭代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你如何建议从某个size_t M迭代到0?


显而易见的方法是:


for(size_t i = M + 1; i> 0; --i){...}


另一种方式是依赖环绕...


有什么建议吗?

[见 http:/ /www.gotw.ca/resources/clcm.htm 了解有关的信息]

[comp.lang.c ++。moderated。第一次海报:做到这一点! ]

How do you suggest iterating from some size_t M down to 0?

The obvious way is:

for(size_t i = M+1; i > 0; --i){...}

Another way is to depend on wraparound...

Any suggestions?
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

推荐答案

u 。******** @ gmail.com 写道:
u.********@gmail.com wrote:
你如何建议从某个size_t M迭代到0?

for(size_t i = M + 1; i> 0; --i){...}

另一种方式是取决于环绕...
How do you suggest iterating from some size_t M down to 0?

The obvious way is:

for(size_t i = M+1; i > 0; --i){...}

Another way is to depend on wraparound...




取决于那个没有问题。在C ++中,无符号整数类型

必须包围。

[见 http://www.gotw.ca/resources/clcm.htm 有关的信息]

[comp.lang.c ++。moderated。第一次海报:做到这一点! ]



There is no problem with depending on that. In C++, unsigned integer types
must wrap around.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


你。***** ***@gmail.com 写道:
你如何建议从某个size_t M迭代到0?


我建议不要这样做。

显而易见的方法是:
for(size_t i = M + 1; i> 0; - -i){...}


哪个不行。你第一次进入i == M + 1

的循环,这当然不是你想要的。

另一种方式是依赖于环绕...... 。
有什么建议吗?
How do you suggest iterating from some size_t M down to 0?
I suggest not doing it.
The obvious way is: for(size_t i = M+1; i > 0; --i){...}
Which doesn''t work. You enter the loop with i == M + 1 the
first time, which certainly isn''t what is wanted.
Another way is to depend on wraparound... Any suggestions?




显而易见的解决方案是使用int或long,所以没有

问题。


另一个明显的观点是,最好是 - 通常,

但特别是在C或C ++中 - 使用半开间隔。所以

将排除M或0。如果是0,没问题。如果它是
是M,我通常的解决方案(即使使用签名类型)是:


int i = M;

while(i> 0){

- i;

// ...

}


-

James Kanze GABI软件

Conseils eninformatiqueorientéeobjet/

Beratung in objektorientierter Datenverarbeitung

9placeSémard,78210 St.-Cyr-l''coco,法国,+ 33(0)1 30 23 00 34

[见 http://www.gotw.ca/resources/clcm.htm 了解有关的信息]

[comp.lang.c ++。moderated。第一次海报:做到这一点! ]



The obvious solution is to use int, or long, so there are no
problems.

Another obvious point is that it is preferable -- in general,
but especially in C or C++ -- to use half open intervals. So
either M or 0 would be excluded. If it is 0, no problem. If it
is M, my usual solution (even when using signed types) is:

int i = M ;
while ( i > 0 ) {
-- i ;
// ...
}

--
James Kanze GABI Software
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l''école, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


你。***** ***@gmail.com ,le 21/03/2006aécrit:
u.********@gmail.com, le 21/03/2006 a écrit :
你如何建议从某个size_t M迭代到0?

显而易见的方法是:

for(size_t i = M + 1; i> 0; --i){...}

另一种方法是依赖环绕......

有什么建议吗?
How do you suggest iterating from some size_t M down to 0?

The obvious way is:

for(size_t i = M+1; i > 0; --i){...}

Another way is to depend on wraparound...

Any suggestions?



size_t i = M + 1;

while( - i< M +1)

{

// ..........

}


-

Pierre Maurette


[见 http://www.gotw.ca/resources/clcm.htm 有关的信息]

[comp.lang.c ++。moderated 。第一次海报:做到这一点! ]


size_t i = M+1;
while(--i < M+1)
{
// ..........
}

--
Pierre Maurette

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


这篇关于使用无符号整数类型进行迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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