fork(2) download
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <vector>
  4.  
  5. static bool comp(int a, int b)
  6. {
  7. //lambda function evaluates to true - no swap,
  8. //evaluates to false -swap
  9. if(a==0) return false;
  10. if(b==0) return true;
  11. //if neither a nor b is 0 them do not swap
  12. return false;
  13. }
  14.  
  15. void moveZeros(std::vector<int>& nums)
  16. {
  17. std::stable_sort(nums.begin(),nums.end(),comp);
  18. }
  19.  
  20. int main(int argc, char** argv)
  21. {
  22. std::vector<int> vec = {0,14,0,3,12};
  23. moveZeros(vec);
  24.  
  25. for(auto v : vec)
  26. std::cout << v << ' ';
  27. std::cout << '\n';
  28. }
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
14 3 12 0 0