fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. #include <vector>
  5. #include <stdexcept>
  6.  
  7. template <typename IntType>
  8. std::vector<IntType> range(IntType start, IntType stop, IntType step)
  9. {
  10. if (step == IntType(0))
  11. {
  12. throw std::invalid_argument("step for range must be non-zero");
  13. }
  14.  
  15. std::vector<IntType> result;
  16. IntType i = start;
  17. while ((step > 0) ? (i < stop) : (i > stop))
  18. {
  19. result.push_back(i);
  20. i += step;
  21. }
  22.  
  23. return result;
  24. }
  25.  
  26. template <typename IntType>
  27. std::vector<IntType> range(IntType start, IntType stop)
  28. {
  29. return range(start, stop, IntType(1));
  30. }
  31.  
  32. template <typename IntType>
  33. std::vector<IntType> range(IntType stop)
  34. {
  35. return range(IntType(0), stop, IntType(1));
  36. }
  37.  
  38. int main() {
  39. cout << range(5, 10, -1).size() << endl;
  40. return 0;
  41. }
Success #stdin #stdout 0s 3140KB
stdin
Standard input is empty
stdout
0