Raw pointer to smart pointer

  c++, smart-pointers

How can I convert the below to smart pointers using unique pointer
Explanation :

Create a Graph with Nodes and add maintain a list of edges.For example
Graph g(4);
g.addEdge(0,1);
g.addEdge(1,2);
g.addEdge(2,3);

List 0
edge : 1
List 1
edge :0 ,1
List 2
edge :0 ,1

class Graph
{
int V;
list<int>* l ;

Graph(int V)
{
    this->V = V;
    l = new list<int>[V]
}

void addEdge(int x,int y)
{
    l[x].push_back(y);
}

my attempt

class Graph
{
int V;
unique_ptr<list<int>> l ;

Graph(int V)
{
    this->V = V;
    // Not sure how to do array of list
    l = make_unique<list<int>[]>[V];
}

void addEdge(int x,int y)
{   
    // same for smart pointer 
    l[x].push_back(y);
}

Please do add guidance how to use smart pointer for dynamic array.
I would like to have addEdge work in same way at it works for raw pointer

Source: Windows Questions C++

LEAVE A COMMENT