Get all stl vector elements greater than a value

2019-07-09 09:43发布

问题:

I would like to know how can I find the list of a stl vector elements that have value verifying a certain condition. For example if I have a vector of int values

vector<int> V;

and I want to get all the elements that are greater than 5.

Thanks in advance.

回答1:

You'd std::copy_if() if the values:

std::vector<int> target;
std::copy_if(v.begin(), v.end(), std::back_inserter(target),
             std::bind(std::less<int>(), 5, _1));


标签: c++ stl vector