Alex Lazar
Alex Lazar

Reputation: 103

What is the difference between initiating a vector as vector < class > ( value ) and vector < class > [ value ]

When my initialization is :
vector < pair < int , int > > v ( 100 );
i can't execute :
vecini[x].push_back( make_pair( y , z ) ) ;
but it works when i initiate it like:
vector < pair < int , int > > v [100];

Upvotes: 0

Views: 421

Answers (2)

Disha Gugnani
Disha Gugnani

Reputation: 34

vector>v(100); initialize a vector of 100 elements where each element is of type pair with all values initialized to 0

vector>v[100]; initializes an array of 100 elements where each element is a vector of type pair

Upvotes: 1

Tarek Dakhran
Tarek Dakhran

Reputation: 2161

You are mixing apples and bananas here: vector initialization and C style array.

using type = vector<pair<int, int>>;
type x(100); // <- single vector with 100 elements in it
type y[100]; // <- array of 100 vectors, with 0 elements in each

Upvotes: 3

Related Questions