Ani V.
Ani V.

Reputation: 65

What is the difference between vector<int> a , vector<int> a[n] and vector<int> a(n)?

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

Answers (2)

anastaciu
anastaciu

Reputation: 23822

vector<int> a; 

Declaration of a vector of ints named a

vector<int> a[n]; 

Declaration of an array of vectors of ints named a with n elements.

vector<int> a(n);

Declaration of a vector of ints a initialized to n number of 0s.

Upvotes: 1

flogram_dev
flogram_dev

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

Related Questions