0

So I have this snippet

vector<int> *adj;
adj = new vector<int>[n];

and this another common way

vector<vector<int> adj(n);

can former way of using pointer be used to make 2d array ? And will it work the same way like latter does?

Also when is former way used if in case it can be used to make 2d vectors.

Can I use former way for making 2d list too?

Incase for those of you who are wondering former method is wrong way of making list it's on geeks for geeks http://www.geeksforgeeks.org/topological-sorting/

8
  • Why would you on purpose want to use pointers? That first code snippet is missing one big piece -- that piece is when to deallocate that vector. The second snippet takes care of all of those details. Commented Dec 16, 2017 at 5:30
  • @PaulMcKenzie I wouldn't, I saw it in someone's codes. Beside, always nice to find new ways of doing things. Commented Dec 16, 2017 at 5:31
  • See my edit to the comment. The first snippet is missing a lot -- it isn't as simple as just calling new. As to std::list, what exactly do you mean by a 2d list? a vector of std::list? Commented Dec 16, 2017 at 5:32
  • @PaulMcKenzie nonetheless former seems to work fine. And no 2d list was like...list of list. Commented Dec 16, 2017 at 5:40
  • 1
    You're missing the point. The former does not work fine if the proper call to delete [] is not made at the proper time. There are thousands of questions on StackOverflow where the poster does the first code, and wonders why Valgrind gives errors, why the destructor crashes, etc. And to a list of list, why not simply compile it and see if it works std::list<std::list<int>> Commented Dec 16, 2017 at 5:43

1 Answer 1

0

There are may ways to create a multidimensional vectors but I use this method which utilizes struct,

Here is an example for 2X2 table

#include<iostream>
#include<vector>

struct table
{
    std::vector<int> column;
};

int main()
{
    std::vector<table> myvec;
    // values of the column for row1
    table t1;
    t1.column = {1,2};
    // Push the first row
    myvec.push_back(t1);
    // values of the column for row2
    table t2;
    t2.column = { 3, 4};
    // push the second row
    myvec.push_back(t2);
    // now let us see whether we've got a 2x2 table
    for(auto row : myvec)
    {
        auto values = row.column; 
        for(auto value : values) std::cout<< value << " ";
        std::cout<<"\n";
    }
    // Now we will try to get a particular value from the column index of a particular row
    table row = myvec[1]; // 2nd row
    std::cout<<"The value present at 2nd row and 1st column is: "<<row.column[0] <<"\n";
}

which gives me,

1 2 
3 4 
The value present at 2nd row and 1st column is: 3

You can easily change it to different dimensions.

Note: I have posted this answer to my knowledge if it is wrong somebody correct me. Thank you

2
  • The same method is applicable to std::list too! Commented Dec 16, 2017 at 6:16
  • okay everyone, really good job. But in my post I wanna understand how my first method works and where it works. Commented Dec 16, 2017 at 12:31

Not the answer you're looking for? Browse other questions tagged or ask your own question.