1

Assume we have the following:

struct MyClass {
   typedef vector<MyValue> InnerVector;
   const InnerVector & get() { ... }
};

vector<MyClass> classes;

Is there a way to provide a "flattened" iterator over classes? That is to view classes as if its some list of InnerVector. Each dereference of the iterator is expected to return an InnerVector. Something like the following:

auto all_inner_vectors = magic_iterator(classes);
for (auto inner_vector : all_inner_vectors) {
  ...
}

It seems that boost iterator adaptors could be handy here. But I could not figure out what adaptors would fit this purpose.

MEE
  • 2,114
  • 17
  • 21
  • Please provide a [mcve] in the sense that the structure of your class is defined. Even though your intent is understandable, it is hard to give anything concrete unless there is a well-defined problem – Passer By May 24 '17 at 04:42
  • You should create a class wrapping `classes` and exposing functionality to obtain a custom `inner vector iterator`. Example of writing something like that: https://gist.github.com/jeetsukumaran/307264 – MABVT May 24 '17 at 06:50
  • And here an awesome answer on how to make it range-based for loop aware https://stackoverflow.com/a/31457319/3754223 – MABVT May 24 '17 at 06:55

1 Answers1

0

With range-v3, you may do

for (MyValue& value : myClass.get() | ranges::view::join) {
    // ...
}

Or you might change your class to use join directly:

struct MyClass {
   using InnerVector = vector<MyValue>;

   // Return a range of MyValue.
   auto get() { return InnerVector | ranges::view::join; }
};

And then

for (MyValue& value : myClass.get()) {
    // ...
}
Jarod42
  • 203,559
  • 14
  • 181
  • 302
  • Though it doesn't directly answer my question (I wanted to iterate on a vector of MyClass, not on the inner vector), but I found range-v3 to perfectly fit my needs. Thanks for pointing it out! – MEE May 24 '17 at 16:20
  • 1
    @Oups, indeed, not applied the join at the correct level. You need `classes | range::view::transform(&MyClass::get) | range::view::join`. – Jarod42 May 24 '17 at 17:17