Chan Kim Chan Kim - 2 months ago 12
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

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};