I have two types of data sets. Both are in same size. One contains vector<int> and other contains vector<vector<double> >.
When I moving forward one element by another, I can see some duplicates from my vector<int>. So, I want to delete the duplicate element (2nd element) from the vector<int> and keep the first element further. If I erase a duplicate element, then, at the same time, I want to merge the vector<double>, of corresponding two vectors, from my vector<vector<double> > data set. Once I merged, i want to delete 2nd vector<double> as it is already added to the previous vector<double>. By doing so, I want to maintain the equal size data sets further without having duplicates in first data set and without loosing any element from my second data set.
I have implemented a simple code for that, but I think its logic is wrong. So, could you please help me to rectify this code?
vector<int> data set is assigned as my_list
vector<vector<double> > data set is assigned as mydata
For example, here is my two vectors
my_list = {222, 208, 201, 201, 201, 206, 211, 222}
mydata = {{a1,a2,a3},{b1,b2},{c1},{d1},{e1,e2},{f1,f2},{g1},{h1,h2,h3}}
after, removing duplicate from the 1st vector and by merging corresponding vectors from
2nd data, the final output would be like as
my_list = {222, 208, 201, 206, 211, 222}
mydata = {{a1,a2,a3},{b1,b2},{c1, d1, e1,e2},{f1,f2},{g1},{h1,h2,h3}}
vector<int>::iterator no, no2;
vector<vector<double> >::iterator itr1, itr2;
int i;
for (no=my_list.begin(), no2=my_list.begin()+1,
itr1=mydata.begin(), itr2=mydata.begin()+1, i=0; no != my_list.end()-1; ){
if (*no == *no2){ //current = next
no2 = my_list.erase(no2);
//add itr2 data to itr1 vector and erase itr2
mydata[i].insert(mydata[i].end(), (*itr2).begin(), (*itr2).end());
itr2 = mydata.erase(itr2);
}
else{
++no; ++no2;
++itr1; ++itr2;
++i;
}
}
I’d better not modify original vectors, if you don’t have any memory issues it is simpler: