Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get all stl vector elements greater than a value

Tags:

c++

stl

vector

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.

like image 776
saloua Avatar asked Nov 26 '25 15:11

saloua


1 Answers

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));
like image 151
Dietmar Kühl Avatar answered Nov 29 '25 04:11

Dietmar Kühl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!