In part of my code, I am using push_back
to form a vector from a txt file:
while (!layerf.eof()) {
for (int i=0; i<kl.nlyr+1; i++) {
std::getline(layerf,line);
depth = atof(line.c_str()) - kl.depth;
hlyr.push_back(depth);
}
}
Let say I input {1,2,3,4}.
later, I need to find a value given as zz lies between which two element of the above vector hlyr, using the following part:
std::vector<double>::iterator loc;
loc = std::upper_bound (hlyr.begin(), hlyr.end(), zz);
Now the problem is hlyr.end is giving me nonsense value, 2 e-98, which is like it has not been intialized! My vector size is fine, even when I look at the vector in "watch' window in visual studio everything is right, but vector end is something nonsense.
If I resize the above vector to 4 nothing changes. But if I resize to 3, it will delete the last cell from the vector, and then the vector.end()
will show 4! It seems vector.end
is pointing to one cell after vector.size
!
Is there an easy fix? I want to use it with the find algorithm I have.