I have a vector of a class Planters
which contain vectors of Plant
s. My goal is to return a vector of plants containing the plants from planter 1, followed by plants from planter 2, etc.
example: planter{{1,2,3,4}, {2,3,4,5}}
should lead to {1,2,3,4,2,3,4,5}
. note that the numbers represent plant objects. I'm trying to use join_view
to flatten it but I am getting the error
error: class template argument deduction failed:
18 | plants = std::ranges::join_view(planterView);
| ^
/home/parallels/CMPT373/se-basic-cpp-template/lib/solutions/task05.cpp:18:52: error: no matching function for call to ‘join_view(std::ranges::ref_view<std::vector<ex4::Planter> >&)’
I have tried the following :
for (auto it : planters){
plants.insert(plants.end(), it.getPlants().begin(), it.getPlants().end());
}
This works however I am only allowed to use one loop (excluding loops inside STL function calls) and can only allocate memory once. The above approach allocates memory multiple times. How do I approach this?
my code:
std::vector<Plant> task05(std::vector<Planter> planters){
std::vector<Plant> plants;
auto planterView = std::views::all(planters);
std::views::transform(planterView, [](Planter planter){ return planter.getPlants();});
plants = ranges::views::all(std::ranges::join_view(planterView));
return plants;
}
Class:
struct Plant {
int plantiness = 0;
Plant(int plantiness)
: plantiness{plantiness}
{ }
bool
operator==(const Plant& other) const noexcept {
return plantiness == other.plantiness;
}
};
class Planter {
public:
Planter(std::initializer_list<Plant> plants)
: plants{plants}
{ }
const std::vector<Plant>&
getPlants() const noexcept {
return plants;
}
private:
std::vector<Plant> plants;
};