Writing an iterator that makes several containers look as one

前端 未结 6 425
遇见更好的自我
遇见更好的自我 2020-12-16 12:12

Consider the following simplified example and desired output:

class A
{
    class combined_iterator
    {
        ????
    }
    typedef ??? t_combined_it;

         


        
6条回答
  •  失恋的感觉
    2020-12-16 12:43

    I think your "naive" approach should work, with the following change: instead of comparing the iterator to the end() of every container, keep a pointer to the current container and compare the iterator only the the current container's end(). When the end has been reached, move on to the next container. That way, you'll never compare the iterator to another container than the one it points to. This will also easily generalize to an arbitrarily-sized collection of collections.

提交回复
热议问题