Reputation: 977
I want to do following thing; I tried, but not succeeded. So I am expecting some way to solve this.
I have two vectors. One is vector<int> myvector
and the other one is vector of Plane
(where Plane
is a created class which stores plane parameters and other properties of planes)
For example I will give you some example for my case; if
myvector = <0, 1, 4, 6, 7, 10, 12, 13, 14, 15, 16, 17>
myvector_new = <plane0, plane1, plane4, plane6, plane7, plane10, plane12, plane13, plane15, plane16, plane17>
(the sizes of above 2 vectors are same.)
I want to modify these 2 existing vectors or create 2 new vectors as such,
myvector_new = <0,1,-1,-1, 4,-1, 6, 7,-1,-1, 10,-1, 12, 13, 14, 15, 16, 17>
myplane_new = <plane0, plane1, null, null,plane4,null,plane6, plane7,null,null, plane10,…., plane17>
(i.e. if I say what I need in another way; first, I want a vector whose size is equal to the maximum element of myvector
. Then I can directly use the indices of this vector to get the inside element values for my other programs, if it is valid value. But, new vector must contain only the above valid values (i.e already existed values). So, I wish to put a negative value to represent other values that I am going to push back or insert. At the same time, I want to modify my second vector myplane
, including only the previously existed valid values of class Plane
and also containing invalid values (like null plane or empty objects).
So, I tried this with vector insert function, but I couldn’t. Then I tried to create new vectors as follows;
//some codes above to create myvector and myplane
Vector<int> myvector_new;
Vector<Plane> myplane_new;
for(int k=0; k<myvector.size(); k++){
if (myvector.at(k)==k){
myvector_new.push_back(k);
myplane_new.push_back(myplane.at(k))
}
else{
for(int m=k; m<myvector.at(k); m++){
myvector_new.push_back(-1);
myplane_new.push_back(null);
}
myvector_new.push_back(myvector.at(k));
myplane_new.push_back(myplane.at(k));
}
}
When I cheked myvector_new (I checked only this sofar) simply as follows;
for(int m=0;m<myplane_new.size();m++){
cout<<myplane_new[m]<<" ";
}
I got the following result. Please anyone help me to modify my codes.
0 1 -1 -1 4 -1 -1 -1 6 -1 -1 -1 7 -1 -1 -1 -1 -1 10 -1 -1 -1 -1 -1 -1 12 -1 -1 - 1 -1 -1 -1 13 -1 -1 -1 -1 -1 -1 14 -1 -1 -1 -1 -1 -1 15 -1 -1 -1 -1 -1 -1 16 -1 -1 -1 -1 -1 -1 17
But, this is wrong. If anyone can help me to modify my codes to be able to get required result, it is highly appreciating.
Upvotes: 0
Views: 125
Reputation: 168836
I'm not sure what is wrong with your code, but there appears to be an easier way to code your algorithm:
// determine appropriate size of result
vector<int>::size_type newsize = myvector.back()+1;
// or, if not sorted:
// vector<int>::size_type newsize = std::max_element(myvector.begin(), myvector.end());
// Create new vectors of the appropriate length
vector<int> myvector_new(newsize, -1);
vector<Plane> myplane_new(newsize);
// Overwrite elements that aren't null
for(vector<int>::size_type i = 0; i < myvector.size(); i++) {
myvector_new[myvector[i]] = myvector[i];
myplane_new[myvector[i]] = myplane[i];
}
Upvotes: 1
Reputation: 2044
Quick code review tells me this is the culprit:
for(int m=k; m<myvector.at(k); m++){
I think you want to set m to myvector.at(k-1) (but be careful about what happens if k is 0).
Upvotes: 0