fork download
  1. #include <iostream>
  2. #include <algorithm>
  3.  
  4. template <typename C>
  5. void ShowElement(C const& elements) {
  6. for (const auto& element : elements) {
  7. std::cout << element << " ";
  8. }
  9. std::cout << '\n';
  10. }
  11.  
  12. struct IsNegative {
  13. template <typename T>
  14. bool operator()(T const& value) {
  15. return value < 0;
  16. }
  17. };
  18.  
  19. struct Greater {
  20. template <class T>
  21. bool operator()(T const& a, T const& b) const {
  22. return a > b;
  23. }
  24. };
  25.  
  26. template <typename C>
  27. void SortElementsInDescendingOrderBeforeFirstNegativeElement(C& elements) {
  28. auto first_negative =
  29. std::find_if(std::begin(elements), std::end(elements), IsNegative());
  30. std::sort(std::begin(elements), first_negative, Greater());
  31. }
  32.  
  33. int main() {
  34. std::vector<int> elements = {4, 7, 3, 4, 5, -2, 1, 0, -1, 8, 9};
  35. std::cout << "Elements before sort:\n";
  36. ShowElement(elements);
  37. SortElementsInDescendingOrderBeforeFirstNegativeElement(elements);
  38. std::cout << "Elements after sort:\n";
  39. ShowElement(elements);
  40. }
  41.  
Success #stdin #stdout 0s 3276KB
stdin
Standard input is empty
stdout
Elements before sort:
4 7 3 4 5 -2 1 0 -1 8 9 
Elements after sort:
7 5 4 4 3 -2 1 0 -1 8 9