Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #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;}())
- #include <iostream>
- #include <vector>
- #include <cassert>
- template <typename Int>
- constexpr auto range(Int a, Int b, Int step) {
- assert(a <= b);
- assert((b-a)%step == 0);
- auto v = std::vector<Int>{};
- for(auto i_ = a; i_ < b; i_ += step) {
- v.push_back(i_);
- }
- return v;
- }
- template <typename Int>
- constexpr auto range(Int a, Int b) {
- return range(a, b, (Int)(1));
- }
- int main() {
- std::vector<float> test = {1.0f, 0.0f, -5.0f, 30.3f, 10.2f, 9.7f};
- for(auto i : range(0, 6, 2)) {
- std::cout << test[i] << "\n";
- }
- std::cout << "\n";
- for(auto i : range(0, 6)) {
- std::cout << test[i] << "\n";
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement