S
spakka
I'm useless at templates, but, inspired by this post:
I'm trying to write an algorithm
template <class BidirectionalIterator, class Function, class Size>
Function for_each_combination(BidirectionalIterator first,
BidirectionalIterator last,
Size k,
Function fn);
which does the obvious thing.
The post references some code (from Feb 05 CUJ) which implements
for_each_permutation. That implementation uses a nice algorithm from
Knuth to generate the permutations which
(i) cycles the elements of [first, last[ in situ, so that the callback can
be called simply as fn(first, last).
(ii) leaves the elements of [first, last[ in their original order.
I can't find such a nice algorithm for combinations. I have a simple
enough algorithm which generates all combinations of the integers 1, ...
(k-1). When it comes to calling the callback fn for each combination, I
copy the corresponding k elements from [first, last[ into a
vector<typename iterator_traits<BidirectionalIterator>::value_type>
but I can't make the call to fn. The caller supplies a callback taking
two parameters of the type BidirectionalIterator, which isn't matched.
My questions:
Is there a way that I can convert iterators from my vector into the type
of the template parameter BidirectionalIterator?
Alternatively, and off-topic, can anyone point me to an algorithm for
enumerating combinations which has the properties (i) and (ii) above?
Thanks.
I'm trying to write an algorithm
template <class BidirectionalIterator, class Function, class Size>
Function for_each_combination(BidirectionalIterator first,
BidirectionalIterator last,
Size k,
Function fn);
which does the obvious thing.
The post references some code (from Feb 05 CUJ) which implements
for_each_permutation. That implementation uses a nice algorithm from
Knuth to generate the permutations which
(i) cycles the elements of [first, last[ in situ, so that the callback can
be called simply as fn(first, last).
(ii) leaves the elements of [first, last[ in their original order.
I can't find such a nice algorithm for combinations. I have a simple
enough algorithm which generates all combinations of the integers 1, ...
(k-1). When it comes to calling the callback fn for each combination, I
copy the corresponding k elements from [first, last[ into a
vector<typename iterator_traits<BidirectionalIterator>::value_type>
but I can't make the call to fn. The caller supplies a callback taking
two parameters of the type BidirectionalIterator, which isn't matched.
My questions:
Is there a way that I can convert iterators from my vector into the type
of the template parameter BidirectionalIterator?
Alternatively, and off-topic, can anyone point me to an algorithm for
enumerating combinations which has the properties (i) and (ii) above?
Thanks.