C++17 zip view and iterator
MIT License
An implementation of zip for C++17. Single header file.
Learning purposes.
In development. Needs proper testing.
Integrate with CMake or just copy the header file.
#include <msd/zip.hpp>
#include <algorithm>
#include <cassert>
#include <vector>
int main() {
std::vector actual_numbers = {1, 2, 3};
std::vector expected_numbers = {1, 2, 3};
const std::size_t size = std::min(actual_numbers.size(), expected_numbers.size());
for (std::size_t i = 0; i < size; ++i) {
assert(actual_numbers[i] == expected_numbers[i]);
}
// vs
for (auto [actual, expected] : msd::zip(actual_numbers, expected_numbers)) {
assert(actual == expected);
}
}
For more, see tests and documentation.
The msd::zip
iterator is bidirectional to better control when you want to stop iterating.
To get only some of the elements from a C++23 zip, you can use std::views::take
:
std::forward_list<int> first_list{1, 2, 3, 4, 5};
std::forward_list<int> second_list{1, 2, 3, 4};
auto forward_zip = std::views::zip(first_list, second_list);
for (auto [a, b] : forward_zip | std::views::take(3)) {
std::cout << a << ", " << b << "\n";
}
With msd::zip
, you can write:
for (auto it = forward_zip.begin(); it != std::prev(forward_zip.end()); ++it) {
auto [a, b] = *it;
std::cout << a << ", " << b << "\n";
}
But it does not work with the mentioned containers.
// Working
for (auto it = zip.begin(); it != zip.end(); ++it) {}
// Not working
for (auto it = zip.begin(); it != zip.end(); it++) {}