Advertisement
Peaser

Fuck

Dec 24th, 2014
361
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.00 KB | None | 0 0
  1. #define range(a, b, step) ([](){assert((step)!=0); assert((1-2*((unsigned)(step)>>31))*(a)<=(1-2*((unsigned)(step)>>31))*(b)); auto v = std::vector<int>{}; for (auto i = (a); (1-2*((unsigned)(step)>>31))*i < (1-2*((unsigned)(step)>>31))*(b); i += (step)) v.push_back(i); return v;}())
  2.  
  3.  
  4. #include <iostream>
  5. #include <vector>
  6. #include <cassert>
  7.  
  8. template <typename Int>
  9. constexpr auto range(Int a, Int b, Int step) {
  10.     assert(a <= b);
  11.     assert((b-a)%step == 0);
  12.     auto v = std::vector<Int>{};
  13.     for(auto i_ = a; i_ < b; i_ += step) {
  14.         v.push_back(i_);
  15.     }
  16.     return v;
  17. }
  18.  
  19. template <typename Int>                    
  20. constexpr auto range(Int a, Int b) {
  21.     return range(a, b, (Int)(1));
  22. }
  23.  
  24. int main() {
  25.     std::vector<float> test = {1.0f, 0.0f, -5.0f, 30.3f, 10.2f, 9.7f};
  26.  
  27.     for(auto i : range(0, 6, 2)) {
  28.         std::cout << test[i] << "\n";
  29.     }
  30.  
  31.     std::cout << "\n";
  32.  
  33.     for(auto i : range(0, 6)) {
  34.         std::cout << test[i] << "\n";
  35.     }
  36.  
  37.     return 0;
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement