vendredi 1 janvier 2016

std::vector randomly adds elements to vector

I am sorting points, the code that I wrote for the sorting part works perfectly, but I have one issue. Every so often (at random times) random points are added to the end of the vector, and when I try to pop_back() to delete these last elements (because I wrote a small portion to check to make sure the number of points are the same), I end up in an infinite loop trying to delete these last elements, which just won't go away. Is there something I should know? Also some of the points randomly disappear even when I do not try to delete them. I am trying to understand what I need to exactly do to prevent these weird points from poping up, because these points are sorted in a specific order.

I only have three insert methods, and the first is an emplace_back(), the next is an insert, and the last is also an insert method:

                if (afterX >= spheres.capacity())
                    spheres.emplace_back(center);
                else if(afterX == -1)
                    spheres.insert(spheres.begin(),center);
                else
                    spheres.insert(std::next( spheres.begin(), afterX ),center);
                elementCount++;

Here is my output, to get an idea:

Point: <5,0,0>
zFind: -1
<5,0,0>
Point: <10,0,0>
zFind: 0
yFind: 0
<5,0,0><10,0,0>
Point: <100,0,0>
zFind: 0
yFind: 0
<5,0,0><10,0,0><100,0,0><2.11207e-023,0,0>
Point: <30,0,0>
zFind: 0
yFind: 0
<5,0,0><10,0,0><30,0,0><100,0,0>
Point: <20,0,0>
zFind: 0
yFind: 0
<5,0,0><10,0,0><20,0,0><30,0,0><100,0,0><2.10934e-023,2.21351e+033,0><0,0,2.21351e+033><0,0,0>

As you can see the point: Point is added correctly in the series (least to greatest), but these are odd extra numbers at the end that are there and then disappear.

Thanks, Simon




Aucun commentaire:

Enregistrer un commentaire