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:

  1. use std::vector<point*>. fill new , empty delete. points live on heap , won't invalidated when vector grows.

  2. call std::vector::reserve size n before push_back points. reserve allocates space vector's data, addresses won't invalidated unless push_back more npoints. std::vector::resize work check documentation first it's different.

edit:

  1. a commenter mentioned saving indices great idea , simpler of this.

Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -