Making a pointer const C++ -


i have 2 vectors of node pointers.

vector<node*> tmp;   vector<node*> nodes; 

and tmp = nodes;

if delete nodes, tmp deleted.

is there way make tmp not modified whenever nodes modified, making const?

i'm running dijkstra's. algorithm deleted nodes vector in determining shortest path. means can shortest path source destination once.

if have graph 0--->1--->2--->3 i.e

cgraph g;  g.dijkstras(0); g.printshortestrouteto(2); g.dijktras(2);          g.printshortestrouteto(3);    <-- source still 0 since nodes deleted                                   therefore path 0 2     class node { public:     node(int id)         : id(id), previous(null),         distancefromstart(int_max)     {         nodes.push_back(this);     } public:     int id;     node* previous;     int distancefromstart; };  vector<node*> nodes;  void cgraph::dijkstras(int source) {      nodes[source]->distancefromstart =  0;          (int = 0; < (int)nodes.size(); i++)         cout << nodes[i]->distancefromstart << " " ;      cout << "------------------------" << endl;      while (nodes.size() > 0)     {         node* smallest = extractsmallest(nodes);         //cout << "smallest: " << smallest->id << " ";         //node* smallest = nnodes[1];         vector<node*>* adjacentnodes =             adjacentremainingnodes(smallest);         const int size = adjacentnodes->size();         (int i=0; i<size; ++i)         {             node* adjacent = adjacentnodes->at(i);             int distance = distance(smallest, adjacent) +                 smallest->distancefromstart;             if (distance < adjacent->distancefromstart)             {                 adjacent->distancefromstart = distance;                 adjacent->previous = smallest;             }         }         delete adjacentnodes;     } } 

you cannot write delete nodes, nodes not pointer. if write that, code not compile.

when write this:

vector<node*> tmp = nodes; 

then make copy of vector object, not contents of vector. contents of vector same in both copies of vector. if change contents 1 vector, change reflect in other copy of vector well.

if want make copy of contents also, write this:

std::vector<node*> tmp; tmp.reserve(nodes.size()):  //include <algorithm> std::transform std::transform(nodes.begin(),                   nodes.end(),                std::back_inserter(tmp),                [](node const *node) { return new node(*node); }); 

note: above code compile in c++11 only, uses lambda expression. in c++03, can use function or functor in place.

consider using smart pointers such std::unique_ptr<node> or std::shared_ptr<node> in place of node*. smart pointers manage memory itself, , need worry it. however, if use smart pointers, above code little different, basic idea same if want make copy of contents.


Comments

Popular posts from this blog

django - How can I change user group without delete record -

java - Need to add SOAP security token -

java - EclipseLink JPA Object is not a known entity type -