Reputation: 123
I'm a real noob in C++ and I've a very simple question.
vector<int> s[10];
What does this declaration actually do? Is s a vector with capacity for 10 integers? What's the difference comparing to this:
vector<int> s(10);
I'm really sorry if this is a stupid question, but I really can't understand what this declarations do.
Thanks!
Upvotes: 3
Views: 145
Reputation: 86
vector<int> s[10];
Means "s" is a array, and it have 10 elements, each of its element's type is vector<int>
.
vector<int> s(10);
Means "s" is a vector<int>
, and this vector's size is 10, but this is not a declaration.
There can be much more complex declarations, when declare a function pointer which point to the kind function who have a lot of parameters and complex return value.
For example:
vector<int> (*func[10])(int *);
This declare the pointer point to a function who receive int *
parameter, and return avector<int>
value, plus it is an array of this kind pointer, the size of the array is 10.
Upvotes: 0
Reputation: 39
Vector has a constructor like this:
vector (size_type n);
So the second is just declare a vector of int with size of 10.
Upvotes: 0
Reputation: 46249
It is a mix of std::vector and built-in array types. Specifically an array of std::vector
;
vector<int> s[10];
means create 10 vector objects. You can think of it like:
vector<int> s0;
vector<int> s1;
...
vector<int> s10;
The number of vector objects is fixed. The number of items in each vector can vary.
Upvotes: 2
Reputation: 63451
The first one is an array of 10 empty vectors.
The second one is a single vector initialised with 10 elements.
Upvotes: 10