Skip to content

Instantly share code, notes, and snippets.

@Ignition
Created November 27, 2012 17:41
Show Gist options
  • Save Ignition/4155787 to your computer and use it in GitHub Desktop.
Save Ignition/4155787 to your computer and use it in GitHub Desktop.
Using Oliver Kowalke's coroutines headers which will soon be in Boost
//https://gitorious.org/boost-dev/boost-dev, coroutine branch
//g++ -I path/to/boost-dev -std=c++11 code.cpp -static -L path/to/boost-dev/stage/lib/ -lboost_context
#include <boost/coroutine/all.hpp>
#include <boost/bind.hpp>
#include <boost/range.hpp>
#include <iostream>
#include <vector>
using namespace std;
using namespace boost;
template <typename T>
using coro_pairT_void = coroutines::coroutine<pair<T&,T&>(void)>;
template <typename T>
void combinations(typename coro_pairT_void<T>::caller_type & self, vector<T> & input ) {
for (auto it1 = input.begin(), itend = input.end(); it1 != itend; it1++) {
for (auto it2 = std::next(it1); it2 != itend; it2++) {
self(pair<T&, T&>(*it1,*it2));
}
}
};
int main( void ) {
vector<int> numbers{1,2,3,4,5,6};
coro_pairT_void<int> func(bind(combinations<int>, _1, numbers));
for (auto it(begin(func)), itend(end(func)); it != itend; ++it) {
cout << it->first << ',' << it->second << endl;
}
return 0;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment