1

When initializing vector of vectors, why don't we write:

    vector<vector<int>> v;
    v.assign(10, vector<int>);

but we need to write:

    v.assign(10, vector<int>());
  • 8
    `vector` is a type, you need to pass a value. – tkausl Feb 14 '19 at 16:02
  • That is a constructor call creating an anonymous object. `assign` requires an object and a number of times to insert it. – DeiDei Feb 14 '19 at 16:02
  • 1
    The declaration initialises the vector. To initialise a vector `v` with that shape, you would write `vector> v(10);`. – molbdnilo Feb 14 '19 at 16:03
  • 1
    Because parameters for [std::vector::assign](https://en.cppreference.com/w/cpp/container/vector/assign) are `count` and `value`. `vector` is a type and not a value. `vector()` is a value (object). It is an empty vector of int values. – Thomas Sablik Feb 14 '19 at 16:12
  • Look this question betrays a basic misunderstanding, but it is very clear and specific. It is not an invalid question. It is not 'too broad'. – Omnifarious Feb 14 '19 at 16:12
  • 1
    Somewhat related but question is posed differently: [How can I create objects while adding them to a vector](https://stackoverflow.com/questions/15802006/how-can-i-create-objects-while-adding-them-into-a-vector) – TrebledJ Feb 14 '19 at 16:17
  • @TrebuchetMS - It's effectively a duplicate, and should be marked as such. Thanks for hunting that down. – Omnifarious Feb 14 '19 at 17:02

1 Answers1

3

You appear to have conflated types with objects (aka values). They are not the same things.

vector<int> is talking about a type, not an actual piece of data. For example, what would you expect to happen in this case?

vector<double> v;

v.assign(10, double);

It's the same thing, except using double as the type name rather than vector<int>. You can even do this:

vector<double> v;

v.assign(10, double());

What double() means is "Construct a new value of type double using the default double constructor.". The default constructor for double happens to give it the value 0.0.

And what vector<int>() means is the same thing. It's saying "Make me a new value of type vector<int> initialized using the default constructor." which happens to make a new vector<int> with 0 elements.

As a side note, what I told you about double having a 'default constructor' is not technically true in the most exacting definition what a constructor is. But, if you're getting confused by the code you're writing right now, that distinction is immaterial. Sort of like learning in your high school chemistry class that electrons technically do not orbit the nucleus.

Omnifarious
  • 54,333
  • 19
  • 131
  • 194
  • 2
    minor nitpick: `double()` does not invoke a constructor, there is no contructor for `double`s – 463035818_is_not_an_ai Feb 14 '19 at 16:16
  • @user463035818 - I do not believe that is true. I think that in C++ all the base types ended up with default constructors. The default constructors for base types are not invoked in all the same situations that default constructors for other types are, but they do exist. Notice how one memory location is initialized and the other not in these examples: https://godbolt.org/z/OWSRkj – Omnifarious Feb 14 '19 at 16:41
  • @Omnifarious see https://stackoverflow.com/questions/5113365/do-built-in-types-have-default-constructors – Alan Birtles Feb 14 '19 at 16:44
  • @AlanBirtles - Oh, so user463035818 is right in the most technical sense. Which is, of course, the best sort of right. Interesting. – Omnifarious Feb 14 '19 at 16:46
  • ...also note the reply from Bjarne himself. haha, seems like i am even too nitpicky for BS :P – 463035818_is_not_an_ai Feb 14 '19 at 16:47