Coder Social home page Coder Social logo

cpp_iterzip's Introduction

iterzip for C++17

Header-only implementation for a python-style zip iterator. Simplifies iteration over multiple containers at once. Supports all standard iterator categories and conforms to std::iterator_traits.

Usage

Supports all iterator-conforming containers. This, of course, includes all STL containers such as std::array, std::vector, std::list etc. You can zip arbitrary many containers and iterate them all at once!

#include "iterzip.hpp"
using iterzip::zip;

std::array<int, 5> a{1,2,3,4,5};
std::array<int, 5> b{6,7,8,9,10};

for(auto [x,y] : zip(a,b)) {
    std::cout << x << ", " << y << std::endl;
}

Will produce the following output

1, 6
2, 7
3, 8
4, 9
5, 10

Containers of different size

For containers with different sizes, the smallest container determines the length of of the zip iteration.

std::array<int, 4> d{1,2,3,4};
std::array<int, 2> e{5,6};

for(auto [x,y] : zip(d,e)) {
    std::cout << x << ", " << y << std::endl;
}

Will produce the following output

1, 5
2, 6

Specifically, two zip iterators are equal (operator!= is false) as soon as at least one of the individual iterators are equal.

Higher iterator acategories

If the containers support it, it is possible to use higher iterator categories' functions. For example, the iterator::operator[] on two std::arrays:

std::array<int, 2> d{1,2};
std::array<int, 2> e{3,4};

auto zipped = zip(d, e);
// only works with random access iterators
std::cout << std::get<0>(zipped.begin()[1]) << std::endl; // outputs 2 (which is the second element in the first container.)

Iterator traits for zip iterators

The zip iterator category depends on the given input containers' iterators. To achieve that, the iterzip namespace provides a natural extension of std::iterator_traits, called iterzip::iterator_traits. The chosen iterator category is always the lowest category of all input iterator types.

using T1 = std::list<int>::iterator;
using T2 = std::array<int>::iterator;
using cat = typename iterzip::iterator_traits<T1,T2>::iterator_category;
// cat == std::bidirectional_iterator_tag;

The zipped value_type is a std::tuple<X, ...> where the X are the individual iterators' value_type. The reference typedef is std::tuple<X&, ...> and pointer is mapped to reference*.

Compiling / Building / Including

In your project, simply include the header file iterzip.hpp and make sure you have support for C++17.

To build the example file provided in the repository, run

$ gcc main.cpp -o main -lstdc++ -std=c++17
$ ./main

The implementation of iterzip uses fold expressions, which are only available after C++17.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.