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

前端 未结 10 682
[愿得一人]
[愿得一人] 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:31

    I think that the simplest way is to use std::accumulate:

    std::accumulate(data.begin(), data.end(), 0, [](int index, float const& value)->int{
        ...
        return index + 1;
    });
    

    This solution works with any container and it don't require a variable or custom classes.

提交回复
热议问题