为什么在C ++中用2个循环变量写一个for循环是如此“困难”? [英] Why is it so 'hard' to write a for-loop in C++ with 2 loop variables?

查看:159
本文介绍了为什么在C ++中用2个循环变量写一个for循环是如此“困难”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:






this:

  int myIndex; 
for(myIndex = 0; myIndex< 10; ++ myIndex)...

C ++开发人员会写这个来防止循环变量泄露到循环外:

  for(int myIndex = 0; myIndex< 10; ++ myIndex)... 
/ pre>

但是,如果你有两个循环变量,你不能再这样做了。以下不编译:

  for(int myIndex = 0,MyElement * ptr = Pool-> First; ptr; ++ myIndex,ptr = ptr-> next)... 

逗号运算符不允许以这种方式定义两个变量,因此我们必须这样写: / p>

  int myIndex; 
MyElement * ptr;
for(myIndex = 0,ptr = Pool-> First; ptr; ++ myIndex,ptr = ptr-> next)...

这违背了具有真实的循环局部变量的优点。



一个解决方案是把整个结构大括号之间,像这样:

  {
int myIndex;
MyElement * ptr;
for(myIndex = 0,ptr = Pool-> First; ptr; ++ myIndex,ptr = ptr-> next)...
}



但这不太优雅。



没有更好的办法这个在C ++(或C ++ 0x)?

解决方案

你只需要理解第一个语句是一个声明逗号是逗号运算符)。这不是更难做:

  for(int i,double d; ...)
  

int i,double d;

因为 for(init cond; expr)语句扩展为:

  {
init
while(cond)
{
语句
expr;
}
}

一个诀窍是让 init 语句结构定义和实例,如:

  for(struct {int myIndex; MyElement * ptr;} data = {0,Pool-> First}; 
data.ptr;
++ data.myIndex,data.ptr = data.ptr-> next)
{
// blah ...
}

  {
struct
{
int myIndex;
MyElement * ptr;
} data = {0,Pool-> First}; bb
$ b while .ptr = data.ptr-> next;
}
}

但我觉得很丑陋。在实践中,我只是像你一样分裂。如果范围是真的一个问题,这可能是不是,在那里扔额外的大括号。



我不认为有很多在这里改进没有一点样板代码。


Possible Duplicate:
In C++ why can’t I write a for() loop like this: for( int i = 1, double i2 = 0; …

A C developer would write this:

int myIndex;
for (myIndex=0;myIndex<10;++myIndex) ...

A C++ developer would write this to prevent the loop variable from leaking outside the loop:

for (int myIndex=0;myIndex<10;++myIndex) ...

However, if you have 2 loop variables, you cannot do this anymore. The following doesn't compile:

for (int myIndex=0,MyElement *ptr=Pool->First;ptr;++myIndex,ptr=ptr->next) ...

The comma operator does not allow two variables to be defined this way, so we have to write it like this:

int myIndex;
MyElement *ptr;
for (myIndex=0,ptr=Pool->First;ptr;++myIndex,ptr=ptr->next) ...

Which defeats the advantage of having real loop-local variables.

A solution could be to put the whole construction between braces, like this:

{
int myIndex;
MyElement *ptr;
for (myIndex=0,ptr=Pool->First;ptr;++myIndex,ptr=ptr->next) ...
}

But this is hardly more elegant.

Isn't there a better way of doing this in C++ (or C++0x)?

解决方案

You just have to understand the first statement is a declaration (and that comma is not the comma operator). It's not any harder to do:

for (int i, double d; ...)

Than it is:

int i, double d;

Because for (init cond; expr) statement gets expanded to:

{
    init
    while (cond)
    {
        statement
        expr;
    }
}

A trick is to make that init statement a struct definition and instance, like:

for (struct { int myIndex; MyElement* ptr;} data = {0, Pool->First};
    data.ptr;
    ++data.myIndex, data.ptr = data.ptr->next)
    {
        // blah...
    }

Which becomes the same as:

{
    struct
    {
        int myIndex;
        MyElement* ptr;
    } data = {0, Pool->First};

    while (data.ptr)
    {
        {
            // blah...
        }
        ++data.myIndex, data.ptr = data.ptr->next;
    }
}

But I find that pretty ugly. In practice, I'd just split it up like you have. If scope is really a problem, which it probably isn't, throw the extra braces around there.

I don't think there's much to improve here without a bit of boilerplate code.

这篇关于为什么在C ++中用2个循环变量写一个for循环是如此“困难”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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