1

I am trying to initialize an array of vectors. How can I do that ?

The following code is wrong:

vector<int> A[] = vector<int>()[10];
dimitris93
  • 4,155
  • 11
  • 50
  • 86
  • 2
    Possible duplicate of [Create and initialise an array of vectors in one go](http://stackoverflow.com/questions/3064595/create-and-initialise-an-array-of-vectors-in-one-go) – Tilak Raj Feb 26 '16 at 21:40
  • @user5871514 I saw this question before, but that just initializes an array of vectors inline. I don't want to do that. – dimitris93 Feb 26 '16 at 21:43
  • `std::vector arrayVectorOfInts[10];` – Tilak Raj Feb 26 '16 at 21:47
  • 1
    @user5871514 I need to do with with the `=` sign, as the `A` variable is a member of a class and I want the code to work inside a constructor. The code of the question is just an example code. – dimitris93 Feb 26 '16 at 21:51
  • A variable is a member of what class?what are you trying to achieve ? update your code a bit , please? – Tilak Raj Feb 26 '16 at 21:55
  • @user5871514 I have a class that contains the array above, named `A`. I initialize it in the constructor of that class. That's all. Your code declares a new variable instead of initializing my array `A`. – dimitris93 Feb 26 '16 at 21:56
  • if you already have an array declared then what you can do is `A.push_back(10); A.push_back(20); A.push_back(30);` something like this can work.if it is already declared and you have a mechanism to put it in the scope. – Tilak Raj Feb 26 '16 at 22:03

3 Answers3

4
std::array<std::vector<int>, 10>

This will gives you an array of 10 with vectors in it.

Luke Xu
  • 2,302
  • 3
  • 19
  • 43
  • Will I need to `delete[]` the array, if you consider that `A` is supposed to be a class variable ? `A = new vector[vertices];` seems more clear to me. – dimitris93 Feb 26 '16 at 21:53
  • You need to delete what's in the array. Meaning you'll need delete 10 times. – Luke Xu Feb 27 '16 at 02:20
2

Try just

vector<int> A[10];

It will default-initialize all ten vector objects.

aschepler
  • 70,891
  • 9
  • 107
  • 161
  • The A array is a variable of a class. I want to do that in the constructor. How can I change the code you gave me so that it uses `=` sign ? – dimitris93 Feb 26 '16 at 21:40
  • I think I was just missing the `new` keyword – dimitris93 Feb 26 '16 at 21:41
  • In that case, just `A{}`. – aschepler Feb 26 '16 at 21:42
  • @Shiro remember that for every `new` you need a `delete` (or a smart pointer). Do not throw in `new`s in a random fashion until you manage to compile, that would result in horrible, horrible, horrible code. – DarioP Feb 26 '16 at 21:49
  • 2
    You probably didn't understand what I said. I have a class `SomeClass` that contains the variable `A` as a private member. I am initializing `A` in the constructor, `SomeClass()`. I was missing the `new` keyword. `A = new vector[vertices];` works fine. Did you suggest something that I didn't understand ? – dimitris93 Feb 26 '16 at 21:49
-1
vector<int>* A = new vector<int>[10];
karan patel
  • 61
  • 1
  • 7