In this example, we compare itertools's adapted ranges with their counterparts from std::ranges
.
#include "./print.hpp"
#include <array>
#include <ranges>
#include <vector>
int main() {
std::vector<int> vec {1, 2, 3, 4, 5, 6};
std::array<char, 2> arr {'a', 'b'};
print_tuple_range(std::views::enumerate(arr));
auto square = [](int i) { return i * i; };
print_simple_range(std::views::transform(vec, square));
print_tuple_range(std::views::zip(vec, arr));
print_tuple_range(std::views::cartesian_product(vec, arr));
print_simple_range(std::views::stride(vec, 2));
print_simple_range(std::views::counted(vec.begin() + 1, 4));
print_simple_range(std::views::iota(10, 20) | std::views::stride(2));
}
auto transform(R &&rg, F lambda)
Lazy-transform a given range by applying a unary callable object to every element of the original ran...
itertools::multiplied< Rs... > product(Rs &&...rgs)
Lazy-multiply a given number of ranges by forming their cartesian product.
sliced< R > slice(R &&rg, std::ptrdiff_t start_idx, std::ptrdiff_t end_idx)
Lazy-slice a given range.
strided< R > stride(R &&rg, std::ptrdiff_t stride)
Lazy-stride through a given range.
zipped< Rs... > zip(Rs &&...rgs)
Lazy-zip ranges together (similar to Python's zip).
enumerated< R > enumerate(R &&rg)
Lazy-enumerate a given range (similar to Python's enumerate).
Output:
(0, a) (1, b)
(0, a) (1, b)
1 4 9 16 25 36
1 4 9 16 25 36
(1, a) (2, b)
(1, a) (2, b)
(1, a) (1, b) (2, a) (2, b) (3, a) (3, b) (4, a) (4, b) (5, a) (5, b) (6, a) (6, b)
(1, a) (1, b) (2, a) (2, b) (3, a) (3, b) (4, a) (4, b) (5, a) (5, b) (6, a) (6, b)
1 3 5
1 3 5
2 3 4 5
2 3 4 5
10 12 14 16 18
10 12 14 16 18