Is there an equivalent to the range-based `enumerate` loop from python in modern C++?

后端 未结 9 1285
终归单人心
终归单人心 2020-12-21 00:48

Is there an equivalent to the range-based enumerate loop from python in C++? I would imagine something like this.

enumerateLoop (auto counter, a         


        
9条回答
  •  执笔经年
    2020-12-21 01:31

    Enumeration of multiple variables has been an idiom since C. The only complication is that you can't declare both variables in the initializer of the for loop.

    int index;
    for (auto p = container.begin(), index = 0; p != container.end(); ++p, ++index)
    

    I don't think it gets any simpler (or more powerful) than that.

提交回复
热议问题