(EPI 1.3.1) Graph problems 16.3 and onwards

#1

I noticed that in Graph problems,
book suddenly started using traditional pointers instead of shared_ptr.
Is there a reason for doing this? Does it make the solution anyway simpler or optimized when it comes to graphs?

e.g.

  struct GraphVertex
  {
        vector<GraphVertex*> edges;
  };

instead of

struct GraphVertex
{
       vector< shared_ptr<GraphVertex>> edges;
};

Also normally arguments to functions are passed by c++ style reference in the entire book but not in Graphs chapter.

e.g.

bool do_something(vector<GraphVertex>* G)
{
     // func def
}

instead of

bool do_something(vector<GraphVertex> &G)
{
     // func def
}

What am I missing?

0 Likes

#2

You can try to modify the * to shared_ptr of course but you will encounter memory ownership problem as those GraphVertex are allocated in stack as you might want to download the code and test it by yourself.

About the pass by reference style, I shall say you might see many people use that but that is not the best practice, it is more like a bad practice in my opinion. Take a look of https://google-styleguide.googlecode.com/svn/trunk/cppguide.html as we also emphasize that in the newer version of EPI also.

1 Like