fork(2) download
  1. #include <algorithm>
  2. #include <iterator>
  3. #include <iostream>
  4. #include <memory>
  5.  
  6. using namespace std;
  7.  
  8. int main() {
  9. std::vector<std::unique_ptr<int>> source, target;
  10. source.emplace_back(new int(1));
  11. source.emplace_back(new int(2));
  12. source.emplace_back(new int(3));
  13. source.emplace_back(new int(4));
  14. source.emplace_back(new int(5));
  15. // your code goes here
  16.  
  17. for(auto const& i : source) {
  18. std::cout << *i << ",";
  19. }
  20. std::cout << endl;
  21.  
  22. auto it = std::stable_partition(begin(source),end(source),[](std::unique_ptr<int> const& val) {
  23. return *val < 3; // only copy values >= 3
  24. });
  25. std::move(it,end(source),std::back_inserter(target));
  26. source.erase(it,end(source));
  27.  
  28.  
  29. std::cout << "after" << endl;
  30.  
  31. for(auto const& i : target) {
  32. std::cout << *i << ",";
  33. }
  34.  
  35. return 0;
  36. }
Success #stdin #stdout 0s 3476KB
stdin
Standard input is empty
stdout
1,2,3,4,5,
after
3,4,5,