I have a class that uses custom logic to generate some sequence:
class Example{
size_t FirstElement();
size_t NextElement(size_t currentelement);
//When element equals magic number this is
//signalling that the sequence is over.
static size_t MagicNumber =-1;
}
I could consume the sequence as follows:
Example example{};
for(size_t curelement = example.FirstElement;
curelement != Example::MagicNumber;
curelement = example.NextElement(currentelement))
{
//do something with the curelement
}
I would like a solution which makes it similarly easy to consume the sequence, but:
- Avoiding the use of the magic number external to Example (i.e. while consuming).
- That does not store the currentelement inside the `example' object.
- That has perhaps a bit cleaner consumtion code in general?
- That does not give substantial performance penalties compared to this code.
- EDIT: Example should not return the whole sequence in one go, i.e. as std::vector.
Is there a good alternative. (Based on my (very limited) understanding of iterators, they should be the goto solution here? If so, how to implement such a solution?)