Reputation: 65
I just learnt about vectors and I'm confused about their use.
Please tell me what is the difference between:
vector<int> a;
,
vector<int> a[n];
and
vector<int> a(n);
Upvotes: 2
Views: 950
Reputation: 23822
vector<int> a;
Declaration of a vector
of int
s named a
vector<int> a[n];
Declaration of an array
of vector
s of int
s named a
with n
elements.
vector<int> a(n);
Declaration of a vector
of int
s a
initialized to n
number of 0
s.
Upvotes: 1
Reputation: 42858
vector<int> a;
declares an empty vector.
vector<int> a[n];
declares an array containing n
empty vectors.
vector<int> a(n);
declares a vector containing n
zeroes.
Bonus:
vector<int> a{n};
declares a vector containing the single element n
.
Upvotes: 7