fork download
  1. #include <iostream>
  2. #include <iterator>
  3. #include <vector>
  4. #include <algorithm>
  5.  
  6. int main() {
  7. int item = 10;
  8. std::vector<int> iArray;
  9. std::generate_n(std::back_inserter(iArray), item , [] ()-> int {
  10. int tmp = std::rand() % 10;
  11. return (std::rand() % 10 > 5) ? tmp : -tmp;
  12. });
  13. std::cout << "Array is: " << std::endl;
  14. std::copy(iArray.begin(), iArray.end(), std::ostream_iterator<int>(std::cout, " "));
  15. std::sort(iArray.begin(), iArray.end(), [](const auto &a, const auto &b) -> bool {
  16. if (a == 0) return true;
  17. return a < b;
  18. });
  19. std::cout << "\nSorted array is: " << std::endl;
  20. std::copy(iArray.begin(), iArray.end(), std::ostream_iterator<int>(std::cout, " "));
  21. return 0;
  22. }
Success #stdin #stdout 0s 16064KB
stdin
Standard input is empty
stdout
Array is: 
3 -7 -3 -6 -9 2 0 3 0 2 
Sorted array is: 
0 0 -9 -7 -6 -3 2 2 3 3