Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

store positive number and negative in different vector

Tags:

c++

vector

I am new to c++. I have a vector with size n. I want to search in the vector ad store positive and negative values in the new vectors. I don't know the number of positive and negative values.

Can anyone help me?

like image 973
H'H Avatar asked Jan 20 '26 18:01

H'H


1 Answers

Here is yet another solution using std::partition_copy from the standard library:

std::vector<int> src, neg, pos;

std::partition_copy(
    src.begin(), src.end(),
    back_inserter(neg),
    back_inserter(pos),
    [](int value){ return value < 0; }
);
like image 79
Blastfurnace Avatar answered Jan 23 '26 07:01

Blastfurnace