2

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);
Community
  • 1
  • 1
Chan Kim
  • 5,177
  • 12
  • 57
  • 112

3 Answers3

6

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};
BiagioF
  • 9,368
  • 2
  • 26
  • 50
0

A multi dimensional vector is declared as a vector of vector:

std::vector<std::vector<int>> multi_dims{};

To initialize a vector with 2 elements, just do

std::vector<int> my_vec = { 1, 2 };
madduci
  • 2,635
  • 1
  • 32
  • 51
-1
int main()
{

    std::vector<std::vector<int>> vec;   //vector of vector for matrix
    vec.push_back({ 10,20,30 }); //first row 
    vec.push_back({ 11,22,33 });  // second row

    for (auto x : vec)                  //printing matrix
    { std::cout << x[0] << " " << x[1] << " "<<x[2]<<std::endl; }

    return 0;
}
Dmytro Dadyka
  • 2,208
  • 5
  • 18
  • 31