fork download
  1. #include <iostream>
  2. #include <iterator>
  3. #include <numeric>
  4. #include <vector>
  5. #include <algorithm>
  6.  
  7. template <typename T>
  8. struct min
  9. {
  10. typedef T element_type;
  11. min(int& min) : min_(min) { }
  12. min& operator *() { return *this; }
  13. min& operator=(const T& t)
  14. {
  15. if (first_)
  16. min_ = t, first_ = false;
  17. else if (t < min_)
  18. min_ = t;
  19. }
  20. min& operator++() { return *this; }
  21. bool first_ = true;
  22. T& min_;
  23. };
  24.  
  25. template <typename Iterator>
  26. struct skip_take
  27. {
  28. skip_take(Iterator i, size_t skip, size_t take = size_t(-1))
  29. : i_(i), skip_(skip), take_(take)
  30. { }
  31. skip_take& operator *() { return *this; }
  32. skip_take& operator=(const typename Iterator::element_type& t)
  33. {
  34. if (skip_ == 0 && take_)
  35. *i_ = t;
  36. }
  37. skip_take& operator++()
  38. { if (skip_) --skip_; else if (take_) --take_, ++i_; return *this; }
  39. Iterator& i_;
  40. size_t skip_, take_;
  41. };
  42.  
  43. int main()
  44. {
  45. std::vector<int> numbers = { 10, 1, 43, 59, 78, 46, 63, 12 };
  46.  
  47. int result;
  48. std::adjacent_difference(numbers.begin(), numbers.end(),
  49. skip_take<min<int>>(min<int>(result), 1));
  50.  
  51. std::cout << result << '\n';
  52. }
Success #stdin #stdout 0s 3272KB
stdin
Standard input is empty
stdout
-51