代码之家  ›  专栏  ›  技术社区  ›  Patrick

为什么用2个循环变量在C++中编写for循环是如此的困难?[副本]

  •  16
  • Patrick  · 技术社区  · 15 年前


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

    C开发人员会这样写:

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

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

    但是,如果有2个循环变量,就不能再这样做了。以下内容无法编译:

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

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

    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)中没有更好的方法吗?

    3 回复  |  直到 8 年前
        1
  •  23
  •   GManNickG    15 年前

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

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

    int i, double d;
    

    因为 for (init cond; expr) statement

    {
        init
        while (cond)
        {
            statement
            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};
    
        while (data.ptr)
        {
            {
                // blah...
            }
            ++data.myIndex, data.ptr = data.ptr->next;
        }
    }
    

    一个问题,它可能不是,扔额外的支架周围。

    如果没有一点样板代码,我认为这里没有什么可以改进的。

        2
  •  8
  •   Peter G.    15 年前

    #include <utility>
    for (auto i = std::make_pair(0,Pool->First);
         i.second;
         ++i.first, i.second=i.second->next)
    
        3
  •  2
  •   Nordic Mainframe    15 年前

    至少C++允许我们在 如果 子句,该子句有时用于声明只有在某些条件为真时才可见的变量:

    if (MyElement *ptr=Pool->First) // block is only entered when ptr!=0
    {
     for (int myIndex=0;ptr;++myIndex,ptr=ptr->next)
     {
     }
    }
    // ptr is out of scope now.