0

I came across this line in reachability.cpp in assignment 1 of Algorithms on Graphs on Coursera where n is num vertices and m is num edges from cin.

vector<vector<int> > adj(n, vector<int>()); 

What is that second argument? I think this makes a 2D vector with n rows, each having a vector(int) but none of the other answers on here mention the second argument and I couldn't find anything on cpp reference and other sources.

I tried to explicity state the size of the columns and initialize with 0 like this:

vector<vector<int> > adj(n, vector<int>(n, 0)); 

because I wanted to print out the shape with the function below to confirm that I have an adjacency matrix.

void print(vector<vector<int> > &a) { std::cout<<"Vec elements:\n"; for(unsigned int j =0; j<a.size(); j++) { for(unsigned int k =0; k<a[j].size(); k++) { std::cout<<a[k][j]<<" "; } } } 

I have other code where I just make a 2D vector like this:

vector<vector<int> > graph; 

Then I resize each row to have n columns with a loop to make the adjacency matrix.

for(i=0;i<n;i++) graph[i].resize(n); //resize 2d array 

Because this is boilerplate code provided by the instructors, there must be some merit to what they are doing. So I'd like to know what it is.

Edit:

Saw this in Gaddis' book:

Gaddis vectors

So I guess it is making a vector of n elements with each element being a vector<int>.

8
  • 5
    This std::vector constructor reference should hopefully be helpful. Commented Jul 26, 2019 at 14:29
  • @Someprogrammerdude I took C++ in school 2 years ago, switched to Java then Python and now it seems I have to start over with Gaddis's textbook. Though we never covered vectors but rather used arrays. Commented Jul 26, 2019 at 14:35
  • 1
    "I couldn't find anything on cpp reference" try this: en.cppreference.com/w/cpp/container/vector/vector (overload 2) Commented Jul 26, 2019 at 14:43
  • vectors are one of the main STL containers, I'd recommend really getting familiar with it, as that's one of the main benefits of C++. I'd imagine that the class was trying to teach C and C++ together, so they never got to vectors. Either that or they were teaching C++98... Commented Jul 26, 2019 at 15:00
  • 2
    I concur with the above. With the possible exception of std::string, you'll find the most-often used class template in your C++ arsenal will be std::vector, and as such it deserves extra study time. Fwiw, in your first example, that second argument is pointless. If omitted, the vector will use default construction for the n elements, which is exactly what std::vector<int>() provides as a temporary copy src. It is more suitable to see this constructor when default construction is not sufficient (eg. your second example), or even possible (such as a type with no default ctor). Commented Jul 26, 2019 at 15:32

1 Answer 1

2

What is that second argument? I think this makes a 2D vector with n rows, each having a vector(int)

Yes, that's right.

The second argument is the "initial value" for each element of your [outer] vector. Here you're saying that each element, which is in turn a vector<int>, will simply be default-constructed. (Well, copy constructed from this default-constructed temporary!)


Note that, contrary to the claims in the other answer, the constructor you're using is not vector( const vector& other, const Allocator& alloc ), but vector( size_type count, const T& value, const Allocator& alloc = Allocator()) (with the third argument left as its default). This question has nothing whatsoever to do with allocators.

It is true, however, that you could also have used explicit vector( size_type count, const Allocator& alloc = Allocator() ) and relied on default-construction of the inner vectors:

vector<vector<int> > adj(n); 
Sign up to request clarification or add additional context in comments.

2 Comments

@Lightning someone said in the comments that it isn't really necessary in the first example I raised in the question but in the second example it would be. The book had the first so I'm guessing they wanted it extra-explicit for teaching purposes.
@mLstudent33 Yep, makes sense.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.