Vector push_back/pop_bakc do not work
May 24, 2016 at 11:41am UTC
In my code I use a vector of arrays. In my function I have to use
push_back
and
pop_back
. If I use my code I get an error message:
vector iterator + offset out of range
.
So my first idea was to comment the push_back line and see what happens. If I call the pop_back methode I get a Mesage:
Expression vector empty before pop.
So this is my code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
std::vector<std::array<double , 3>>::const_iterator closestPoint(const std::array<double , 3>& point, const std::vector<std::array<double , 3>>& matrix)
{
double bestDistance = DBL_MAX;
auto bestIt = matrix.end();
for (auto it= matrix.begin(); it!= matrix.end(); ++it)
{
const auto distance = squareDistancePoints(point, *it);
if (distance < bestDistance)
{
bestDistance = distance;
bestIt = it;
}
}
return bestIt;
}
int exportTxt(
const std::vector<std::vector<LineSegment>> &slicesWithLineSegments,
const v3 &aabbSize) {
const size_t nSlices = slicesWithLineSegments.size();
const size_t slicePerRow = (size_t)sqrt((float )nSlices);
std::vector<std::array<double , 3>> matrix;
std::vector<std::array<double , 3>> hull;
int spalte=3;
unsigned int zeile;
for (size_t i=0; i<nSlices; ++i) {
const std::vector<LineSegment> &lss = slicesWithLineSegments[i];
zeile = 2*lss.size();
matrix.resize(zeile);
int j=0;
for (size_t i = 0;i<lss.size();i++) {
int k = j+1;
matrix[j][0]=lss[i].v[0].x;
matrix[j][1]=lss[i].v[0].y;
matrix[j][2]=lss[i].v[0].z;
matrix[k][0]=lss[i].v[1].x;
matrix[k][1]=lss[i].v[1].y;
matrix[k][2]=lss[i].v[1].z;
j=j+2;
}
hull.push_back(matrix.back());
matrix.pop_back();
while (!matrix.empty())
{
auto it = closestPoint(hull.back(), matrix);
hull.push_back(*it);
matrix.erase(it);
}
}
file.close();
unsortedfile.close();
return 0;
}
I just do not know, what I'm doing wrong. Can someone help me?
Last edited on May 24, 2016 at 11:47am UTC
May 24, 2016 at 11:45am UTC
auto it = closestPoint(hull.back(), matrix);
Is the returned oterator valid?
May 24, 2016 at 11:49am UTC
@Moschops: Sorry I forgot this function. I edited my question. But I don't think this is the problem, because I receive the same error massages, if I delete the while-loop
May 24, 2016 at 12:20pm UTC
Point at the push_back that provokes the error message so we can see which push_back you're talking about.
Last edited on May 24, 2016 at 12:21pm UTC
May 24, 2016 at 12:22pm UTC
As soon as zeile
is 0 you must not execute line 53/54.
Topic archived. No new replies allowed.