c++ - How can I initialise the vector of vectors of ints? -


if have 2-d vector (basically representing graph first column being initial vertex , second column being final vertex), this, vector<vector<int> > myvec;

and want store this,

0 -> 1 2  1 -> 2 3 2 -> 3 4 3 -> 4 5 5 -> 1 2 

if want initialise myvec, is, total number of rows (number of vertices) , can insert second vertex @ appropriate position, i.e. after initialisation (if enter number of vertices 6), want this,

0 -> 1 -> 2 -> 3 -> 4 -> 5 -> 

and can insert appropriate edge using,

myvec[startingvertex].push_back(endingvertex) // starting vertex , ending vertex taken input 

how can it? thanks!

edit: have:

class graph {     int vertices;     vector<vector<int> > edges; } 

in main, following

int main (void) {     int i,n;     cout<<"how many vertices wish enter\n";     cin>>n;     graph *mygraph = new graph;     mygraph->vertices = n;     mygraph->edges // how can initialise here?     ......... // rest of code } 

can me above commented question?

thanks!

// create myvec 6 elements in it. vector<vector<int> > myvec(6);  // add items first item of myvec myvec[0].push_back(1); myvec[0].push_back(2);  // add items sixth item of myvec myvec[5].push_back(10); myvec[5].push_back(25); 

update

implement constructor of graph follows:

class graph {    public:       grapah(int n) :  edges(n) {}        vector<vector<int> > edges; }; 

and use as:

graph *mygraph = new graph(n); 

you don't need member variable vertices since edges.size() number of vertices.


Comments

Popular posts from this blog

python - Healpy: From Data to Healpix map -

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -