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

后端 未结 9 1291
终归单人心
终归单人心 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:17

    Tobias Widlund wrote a nice MIT licensed Python style header only enumerate (C++17 though):

    GitHub

    Blog Post

    Really nice to use:

    std::vector my_vector {1,3,3,7};
    
    for(auto [i, my_element] : en::enumerate(my_vector))
    {
        // do stuff
    }
    

提交回复
热议问题