Hello!
I'm writing some mathematical iterators over some big classes that I don't want to store in memory at all, but I still want to be able to iterate through them. As an example (but I have a few of these), think of a class Combinations({0,1,2...7}, 3)
, which means I would like to iterate over all subsets of size 3 from the set {0,1,2...7}
I already have the code for this, that's not my problem. The thing is, I'd like to make it compatible with the STL. Right now, I have only one class, which is called "CombinationsIterator" that has a function called bool Next(vector<int>& output)
and when called, it kinda acts as std::next_permutation(output), just modifies output so that it represents the next combination. And so if I want to iterate over everything, I do this:
CombinationIterator<int> X(somevectorofints, 3);
vector<int> combination = X.First(); //I defined this in combinationiterator
do
{
//operate with combination
} while (X.Next(combination)); //next returns false if there are no more combinations
Don't get me wrong: this works perfectly for now. But I'd like to make it compatible with the STL so that I get access to tons of neat functions like find_if() and so on.
As I said, one would never want to hold all combinations in memory.
Now, before someone starts to say "oh, but combinations is in such and such library...", yeah, maybe, but I have a bunch of those for many other mathematical objects.
What would this mean? Probably change next to ++it
, but then what is an iterator? does it hold the "current combination" on its own, and calling *it
returns a reference to current_combination?
But then when I copy the iterator (like it1 = it2
) (I think iterators are supposed to be copyable), should it copy the whole thing? Isn't that a waste? Is there a smarter way to approach this? And what would the iterator "end()
" be? begin()
is easy. What's a comparison between iterators? Would I need to compare the whole thing?
I'm confused...
Thank you!