c++ - How to prevent memory reallocation using std::vector -
i've read lot of question no 1 answer me specific case.
actually have
std::vector<point2dd> points; std::vector<triangle> triangles; point2dd class 2d point, not important specify how implemented.
triangle implemented like:
class triangle{ public: triangle(); triangle(point2dd* p1, point2dd* p2, point2dd* p3); // getter & setter private: point2dd* va = nullptr; point2dd* vb = nullptr; point2dd* vc = nullptr; } that is, three-pointers vector of points.
actually work i've think: if add other point vector , vector change memory address? triangles composed invalid address.
i've read using std::unique_ptr<point2dd> don't think best way.
have solution? :)
--- edit 1 ---
to clarify problem explain problem i'm trying solve. i'm doing incremental delaunay triangulation (no problem that). have add once once point , update triangulation.
so i've think manage triangle 3 pointer points. have dag (node -> triangles 3 children) , structure save adjacent triangles.
this why i've thinked use pointer, don't have copy in 3 different structures same points.
this why need solve problem prevent memory reallocation.
two simple options:
use
std::vector<point*>. fillnew, emptydelete. points live on heap , won't invalidated when vector grows.call
std::vector::reservesizenbeforepush_backpoints.reserveallocates space vector's data, addresses won't invalidated unlesspush_backmorenpoints.std::vector::resizework check documentation first it's different.
edit:
- a commenter mentioned saving indices great idea , simpler of this.
Comments
Post a Comment