#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 << '\n';
    
    return 0;
}

 

相关文章:

  • 2022-12-23
  • 2021-11-15
  • 2022-12-23
  • 2021-08-01
  • 2021-07-20
  • 2021-09-10
  • 2022-02-24
  • 2022-12-23
猜你喜欢
  • 2021-06-14
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-08
  • 2021-07-30
  • 2021-09-25
相关资源
相似解决方案