Boost logo

Boost Users :

From: Lars Sunde (laasunde_at_[hidden])
Date: 2020-04-05 19:31:32


Hello,

How can I create a Boost Range for multiple vectors without setting any limitation to the number of vector at compile time?

Using Boost 1.70 and it is not an option to store all data in one vector.

The following code compile on Visual Studio 2017 and will output the number 1 to 9. However, this code is compiled to support 3 vectors inside the foo struct. In order to support 4 vectors I would need to re-compile the code. The number of vectors is only known at runtime. How can this code be improved to support a dynamic number of vectors ?

#include <iostream>
#include <vector>
#include <boost/range.hpp>
#include <boost/range/join.hpp>

struct foo
{
  typedef boost::range::joined_range<
    boost::range::joined_range<
      std::vector<int>,
      std::vector<int>>,
    std::vector<int>> range;

  std::vector<std::vector<int>> vec;

  foo()
  {
    vec = { {1,2,3}, {4,5,6}, {7,8,9} };
  }

  range my_range()
  {
    auto range1 = boost::join(vec[0], vec[1]);
    return boost::join(range1, vec[2]);
  }
};

int main()
{
  foo f1;
  for (auto item : f1.my_range())
    std::cout << item << " ";

  return 0;
}

Appreciate any input.

Kind regards, Lars



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net