#include <iostream> // cout #include <algorithm> // copy_if, distance #include <vector> // vector using namespace std; int main () { vector<int> foo = {25,15,5,-5,-15}; vector<int> bar (foo.size()); // copy only positive numbers: auto it = copy_if (foo.begin(), foo.end(), bar.begin(), [](int i){return !(i<0);} ); bar.resize(distance(bar.begin(),it)); // shrink container to new size cout << "bar contains:"; for (int& x: bar) cout << ' ' << x; cout << ' '; return 0; }