mirandaconrado / product-iterator Goto Github PK
View Code? Open in Web Editor NEWC++ iterator that performs the cartesian product of many containers.
License: MIT License
C++ iterator that performs the cartesian product of many containers.
License: MIT License
First, let me thank you the for this. Seems exactly what I'm looking for.
Have you considered what happens/should-happen if one of the containers are empty?
I get a crash when trying to work with that, e.g. as illustrated:
#include "product_iterator.hpp"
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> c1({1,2});
vector<char> c2; // NOTE THIS IS EMPTY!
auto it = make_product_iterator(c1, c2);
auto end = it.get_end();
for(; it != end; ++it)
{
cout << "{" << it.get<0>() << ", " << it.get<1>() << "}\n";
}
}
To allow client to use/test for that you could e.g. introduce a new method - similar to the get<>()
- that tests for whether the container iterator is at its end, e.g. something like:
template <size_t I>
bool not_end() {
return std::get<I>(current_)!=std::get<I>(end_);
}
I've tried to add that (se snippet code below for usage), but it still crashes al beit after output the first expected combination. I haven't examined (yet) what is causing the crash, but likely it is related to how you advance iterators in your implementation.
for(; it != end; ++it)
{
cout << "{" << it.get<0>();
if( it.not_end<1>() ) cout << ", " << it.get<1>();
cout << "}\n";
}
Expected output:
{1}
{2}
Regards,
/Michael
Just wanted to say this is awesome! Thanks! :)
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.