0

I wrote a container based on std::vector<std::vector<T> >. Now I would like to have an iterator for it so I could go process it row-wise in a way I walk through a std::vector. What is the shortest std (no boost this time) way to achieve this?

EDIT:

Actually, std::vector<std::vector<T> > is not exactly what I have. My code looks similar to this:

    struct Soldier { std::string name;};

    class Platoon {
      public: Soldier* commander;
      std::vector<Soldier> soldiers;
    };

    class Company {
      public: Soldier* commander;
      std::vector<Platoon> platoons;
    };

    class Batalion {
      public: Soldier* commander;
      std::vector<Company> companies;
    };

I have five levels of hierarchy in total, including soldiers. I would like each of these classes to have begin() and end() methods allowing iteration over soldiers, e.g. in order to:

    std::for_each(bravo_company.begin(), bravo_company.end(), 
          front_line());
tnorgd
  • 1,580
  • 2
  • 14
  • 24
  • 4
    Are you trying [to flatten the containers](http://stackoverflow.com/questions/3623082/flattening-iterator)? – James McNellis Apr 02 '14 at 20:10
  • I would say so. The problem in question is like a hierarchy of nested containers: a brigade contains batalions, a batalion: companies, a company: platoons; a platoon - soldiers. I want to list all soldiers for each level of the hierarchy – tnorgd Apr 02 '14 at 20:37
  • In that hierarchy there's only one level of soldiers (the bottom). What do you mean by "for each level"? Did you mean just "list all the soldiers" ? – M.M Apr 02 '14 at 20:50
  • +1 @James for remembering it's duplicate of an answer he wrote 3.5 years ago – M.M Apr 02 '14 at 20:51
  • I know this is an overkill rather than the shortest way, but I recently wrote an [iterator](https://github.com/iavr/ivl2/blob/master/include/ivl/root/core/array/iter/join.hpp) for a [`join` view](https://github.com/iavr/ivl2/blob/master/include/ivl/root/core/array/view/join.hpp) of an arbitrary number of arbitrarily-typed sequences or sequence views. E.g. this enables writing `join(a, b, join(c, d), join(e, f, g))` and seeing everything as a flat sequence. If the value types are the same, it even allows read/write access, so can be used as an lvalue, e.g. `join(a, b) = join(c, d, e)`. – iavr Apr 02 '14 at 22:03

0 Answers0