Is there a simple way to add a vector into another vector, and to delete the iterator at the insertion position afterwards (practically "replacing the iterator" with a new vector)? What i wish to do is (read comment in Code):
struct Data
{
Data(int i) :d(i) {}
vector<Data> vec;
int d;
};
vector<Data> dataVector = { Data(1), Data(2), Data(3) };
dataVector[1].vec = { Data(41), Data(42) };
// Task: replace Data(2) in dataVector with dataVector[1].vec to get {Data(1),Data(41),Data(42),Data(3)};
for (auto it = dataVector.begin(); it != dataVector.end();) {
if ((*it).d == 2) {
it = dataVector.insert(it,(*it).vec.begin(),(*it).vec.end());
// Now delete Data(2) somehow
// it = dataVector.erase(...
}
else it++;
}
is there an elegant way to achieve this without incrementing "it" n times or using additional variables? In general I would have vectors of arbitrary length, and I would prefer to add append the new vector at the end, but that is not crucial.
I just found a solution:
it = dataVector.insert(it+1,it->vec.begin(),it->vec.end());
it = dataVector.erase(it-1);
Maybe it is not the best one, but it seems to work even for insertinf at the beginning/end of the vector. Thanks for your help!