How to get the index of a value in a vector using for_each?

前端 未结 10 721
[愿得一人]
[愿得一人] 2020-12-28 14:02

I have the following code (compiler: MSVC++ 10):

std::vector data;
data.push_back(1.0f);
data.push_back(1.0f);
data.push_back(2.0f);

// lambda          


        
10条回答
  •  臣服心动
    2020-12-28 14:22

    In C++14 thanks to generalized lambda captures you can do something like so:

    std::vector v(10);
    std::for_each(v.begin(), v.end(), [idx = 0] (int i) mutable {
        // your code...
        ++idx; // 0, 1, 2... 9
    });
    

提交回复
热议问题