user466534
user466534

Reputation:

Vector declaration type in c++

Please can anybody explain to me what this means?

vector<int> myvector(4,99);  

Upvotes: 1

Views: 359

Answers (2)

KeatsPeeks
KeatsPeeks

Reputation: 19327

A a(x,y); creates an object called a, calling a constructor of A with two parameters matching the types of x and y, or any convertible types.

So this:

vector<int> myvector(4,99);

Matches this constructor:

explicit vector( size_type num, const TYPE& val = TYPE() ); 
// `TYPE` is a `typedef` assigned to the parametrized type (here `int`), which means the constrcutor is actually:
explicit vector( size_type num, const int& val = int() );

Which constructs a vector with 4 elements of value 99 and calls it myvector. This constructor is called because the first parameter can be converted to a size_type, which is also a typedef, defined to an integral type (usually unsigned long).

Upvotes: 10

Matti Virkkunen
Matti Virkkunen

Reputation: 65116

It's (most likely) an std::vector of integers, initialized to contain four integers with the value of 99.

Upvotes: 11

Related Questions