0

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
  • 4,339
  • 10
  • 39
  • 84

3 Answers3

5

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
  • 8,996
  • 2
  • 23
  • 48
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,467
  • 1
  • 31
  • 44
-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,115
  • 5
  • 16
  • 30