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?

Community
  • 1
  • 1
vuvume
  • 125
  • 1
  • 7
  • 1
    Voting to close as a typo. get rid of the constructor call since the initializer list replaces that. `vector< vector > m = ...` – NathanOliver Jan 06 '17 at 16:31

2 Answers2

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}
    }};
Community
  • 1
  • 1
François Andrieux
  • 28,148
  • 6
  • 56
  • 87
  • 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 Jan 06 '17 at 16:33
  • @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çois Andrieux Jan 06 '17 at 16:34
  • Is using `std::array` the same as doing `int m[3][3]`? – vuvume Jan 06 '17 at 16:36
  • @vuvume No, `std::array` is different from c arrays like `int m[3][3];`. `std::array` is a class much like `std::vector`. – François Andrieux Jan 06 '17 at 16:37
  • 1
    @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[]` – NathanOliver Jan 06 '17 at 16:38
  • If I do `std::array, 3> arr = {{},{},{}};`, is it valid to check `arr[2][2] == null`? If not, how would I check for null elements? – vuvume Jan 06 '17 at 16:39
  • @NathanOliver I'm just trying to avoid any confusion where @vuvume would interpret my comment as recommending `int m[3][3]`. – François Andrieux Jan 06 '17 at 16:40
  • @vuvume That is fine since any missing initializer is turned into a zero initializer. – NathanOliver Jan 06 '17 at 16:41
  • 1
    @vuvume: What book are you using? The Stack Overflow comments section is not the proper place to learn C++. – Lightness Races in Orbit Jan 06 '17 at 16:42
  • 1
    @vuvume, there are _no_ null elements in an empty data container, and accessing a nonexistent element will result in undefined behavior. – ForceBru Jan 06 '17 at 16:49
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.

ForceBru
  • 43,482
  • 10
  • 63
  • 98
  • What if I want to initialize an empty 3x3 array? How would I access `test[2][2] == null`? – vuvume Jan 06 '17 at 16:35
  • @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. – Lightness Races in Orbit Jan 06 '17 at 16:40