0

How can I create an NxM 2D int vector and create default values for it?

Here I try to create a 3x3 int vector with some values:

vector< vector<int> > m(3, vector<int> (3)) = {
    {1,2,9},
    {8,4,7},
    {5,6,0}
   };

But this errors with

> g++ a.cpp -std=c++11

error: expected ‘,’ or ‘;’ before ‘=’ token
     vector< vector<int> > m(3, vector<int> (3)) = {
                                                 ^
error: expected ‘}’ at end of input
 }

I am using c++11 also, so shouldn't the above syntax be correct? According to this answer, it should be okay?

1
  • 1
    Voting to close as a typo. get rid of the constructor call since the initializer list replaces that. vector< vector<int> > m = ... Commented Jan 6, 2017 at 16:31

2 Answers 2

1

It works fine if you remove what's in the parenthesis. The dimensions are determined by the size of the initializer lists. If you want to specify the size yourself, you can use std::array.

std::vector< std::vector<int> > m= {
    {1,2,9},
    {8,4,7},
    {5,6,0}
   };

Initializing arrays is a bit different. See this question. You need double braces.

#include <array>
std::array< std::array<int, 3>, 3 > m= {{
    {1,2,9},
    {8,4,7},
    {5,6,0}
    }};
Sign up to request clarification or add additional context in comments.

10 Comments

What is the difference between using std::array and std::vector when declaring a 2D array? When would you use one over the other?
@vuvume The size of an std::array is specified at compile time and can never change. The size of an std::vector can change at runtime.
@FrançoisAndrieux Not really. A std::array is just a wrapper for T[]. It has the same aggregate initialization. It just adds some convenience functions and makes passing the array "easier". Yes it is a class type but it really is basically a drop in replacement or T[]
@vuvume: What book are you using? The Stack Overflow comments section is not the proper place to learn C++.
@vuvume, there are no null elements in an empty data container, and accessing a nonexistent element will result in undefined behavior.
|
0

The code in parentheses provides arguments for vector's constructor:

vector<vector<int>>(3, vector<int>(3));

This creates a new vector that contains three elements of type vector, and each of them has got three integers inside. So, no nulls inside a vector in this case.

This code:

vector<vector<int>> test = 
    { {3,4,5},
       {4,5,6} };

Uses another constructor, which takes an initializer list and deduces the size of the resulting vector by itself.

Those are different constructors! When you construct an object with the Type object(arg1, arg2); notation, you're basically calling the constructor of Type. You're calling a function, and of course you cannot adding to a function call.

2 Comments

What if I want to initialize an empty 3x3 array? How would I access test[2][2] == null?
@vuvume: null?? Stop randomly guessing - programming doesn't work that way. Anyway, there is no such thing as an empty 3x3 array; an array either has elements, or it doesn't.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.