对于与 [英] for vs. while

查看:65
本文介绍了对于与的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我读到的地方最好用而不是循环使用

这种情况​​如下:


vector< mytypevmt;


而不是

for(int j = 0; j< static_cast< int(vmt.size()) ; j ++)

vmt [j] .method(args);


int j = 0;

while(j< ; static_cast< int(vmt.size()))

vmt [j ++]。method(args)

这没关系,也不是[j ++]使用& ; J型之前将它增加到

下一个。


谢谢

解决方案

Gary Wessle写道:


我读到了一个更好用的地方而不是for循环

这种情况​​如下:


vector< mytypevmt;


而不是

for(int j = 0; j< static_cast< int(vmt.size()); j ++)

vmt [j] .method(args);


int j = 0;

while(j< static_cast< int(vmt.size()))

vmt [j ++]。method(args)


是这没关系,也不是使用j的[j ++]。然后在下一个增加到

之前。



我通常写(/< mytype> :: size_type j = 0,s = vmt.size)
); j< s; ++ j)

vmt [j] .method(args);





for(vector< mytype> :: iterator it = vmt.begin(),e = vmt.end();

it!= e; ++ it)

(* it).method(args);





vector< mytype> :: iterator it = vmt。 begin(),e = vmt.end();

while(it!= e)

(* it ++)。method(args);


,取决于我当时的感受。使用''int''并且执行

''static_cast''绝对不正常。


现在,关于你在什么地方读到什么? ,我不确定,你怎么用
定义更好?


V

-

请在通过电子邮件回复时删除资金''A'

我没有回复最热门的回复,请不要问


Gary Wessle< ph **** @ yahoo.comwrote:


我读到的地方最好用而不是循环在

这个案例如下:


vector< mytypevmt;


而不是

for(int j = 0; j< static_cast< int(vmt.size()); j ++)

vmt [j] .method(args);


int j = 0;

while(j< static_cast< int(vmt.size()))

vmt [j ++]。method(args) )


这是好的,也不是使用j的[j ++]。然后在下一个增加到

之前。



嗯,我个人认为在这种情况下使用随机访问是一个很小的关闭。更好的方法是使用迭代器并通过它递增。


但是要回答你给出的问题。我希望看到某个地方。那个

你读它,因为它对我来说似乎很傻。


1)在前一种情况下,''j'的范围包含在for循环

块,而在后一种情况下它延伸到

函数的其余部分。


2)In在前一种情况下,循环变量包含在一行代码中,而在后一种情况下,初始化,循环结束和

增量在单独的行中。


为什么在这个特殊的

情况下,人们更喜欢while循环而不是for循环?


-

要给我发电子邮件,请输入sheltie主题。


2006年10月27日10:49:24 +1000 in comp.lang.c ++,Gary Wessle

< ph **** @ yahoo.comwrote,


>我读到的地方更好用,而不是循环使用
这种情况如下:



使用对你来说最明确,最直接的表达方式

你想说的话。这与

编译器完全相同。


当一个简单变量从一个值增加时,我喜欢使用''for'' />
到另一个,就像你的例子中的旧FORTRAN DO循环一样;和

'''''当模式更复杂时。


Hi

I read somewhere that is better to use while instead of for loop in
this case below:

vector<mytypevmt;

instead of
for ( int j=0; j < static_cast<int(vmt.size()); j++)
vmt[j].method(args);

int j = 0;
while ( j < static_cast<int(vmt.size()) )
vmt[j++].method(args)
is this ok, also not the [j++] which uses "j" before increments it to
the next one.

thanks

解决方案

Gary Wessle wrote:

I read somewhere that is better to use while instead of for loop in
this case below:

vector<mytypevmt;

instead of
for ( int j=0; j < static_cast<int(vmt.size()); j++)
vmt[j].method(args);

int j = 0;
while ( j < static_cast<int(vmt.size()) )
vmt[j++].method(args)
is this ok, also not the [j++] which uses "j" before increments it to
the next one.

I usually write

for (vector<mytype>::size_type j = 0, s = vmt.size(); j < s; ++j)
vmt[j].method(args);

or

for (vector<mytype>::iterator it = vmt.begin(), e = vmt.end();
it != e; ++it)
(*it).method(args);

or

vector<mytype>::iterator it = vmt.begin(), e = vmt.end();
while (it != e)
(*it++).method(args);

, depending on how I feel at the time. Using ''int'' and doing the
''static_cast'' is definitely not normal.

Now, as to what you "read somewhere", I am not sure, how do you
define "better"?

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


Gary Wessle <ph****@yahoo.comwrote:

I read somewhere that is better to use while instead of for loop in
this case below:

vector<mytypevmt;

instead of
for ( int j=0; j < static_cast<int(vmt.size()); j++)
vmt[j].method(args);

int j = 0;
while ( j < static_cast<int(vmt.size()) )
vmt[j++].method(args)
is this ok, also not the [j++] which uses "j" before increments it to
the next one.

Well, personally I think using random access in this situation is a
little off. Better would be to use an iterator and increment through it.

But to answer your given question. I''d like to see that "somewhere" that
you read it because it seems quite silly to me.

1) In the former case, the scope of ''j'' is contained in the for loop
block, while in the latter case it extends throughout the rest of the
function.

2) In the former case, the looping variable is contained within one line
of code, while in the latter case, initialization, loop end, and
increment are on separate lines.

Why would one prefer the while loop over a for loop in this particular
situation?

--
To send me email, put "sheltie" in the subject.


On 27 Oct 2006 10:49:24 +1000 in comp.lang.c++, Gary Wessle
<ph****@yahoo.comwrote,

>I read somewhere that is better to use while instead of for loop in
this case below:

Use the one that to you is the clearest and most direct expression
of what you are trying to say. It''s exactly the same to the
compiler.

I like to use ''for'' when a simple variable increments from one value
to another, like an old FORTRAN DO loop, as in your example; and
''while'' when the pattern is more complex.


这篇关于对于与的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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