Chan Kim Chan Kim - 1 year ago 118
C++ Question

STL vector initialization with two arguments

I saw How to initialize a vector in c++ but couldn't find the same case so I ask here.

What is this expression? It's not two dimensional vector(I mean vector of vector). Is it declaring a vector with two elements?

vector<int> mult_dims(1, 2);

Answer Source

Just read documentation.

explicit vector (size_type n, const value_type& val);

fill constructor: Constructs a container with n elements. Each element is a copy of val.

You code

 vector<int> mult_dims(1, 2);

Constructs a vector with one element with the value 2.

It's equivalent to:

  std::vector<int> NO_mult_dims = {2};
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download