This is (yet) a(nother) follow up to James' answer to this question: Flattening iterator
How do I alter the flattenig_iterator such that it works recursively? Say I have more levels of nested containers and I don't want to be limited to a given nesting depth. I.e. flattening_iterator should work with
std::vector< std::vector < std::vector < int > > >
as well as with
std::vector< std::vector < std::vector < std::vector < int > > > >
In my actual code I have an array of objects which might or not contain such an array themselves.
edit:
After playing around with different ways of iterating through different kind of nested containers I learned something that might be interesting to others as well:
Accessing the container elements with nested loops executed 5 to 6 times faster than with the iterator solution.
Pros:
Cons:
Other pros and cons?
I'll quickly outline a solution:
is_container
trait that detects begin()
and end()
members, or possibly some more complex rules;all_flattening_iterator<T>
template that is just a flattening_iterator<all_flattening_iterator<typename T::value_type>>
;all_flattening_iterator<T>
for when T
is not a container (use a default template bool
parameter) that is just a regular iterator.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With