0

I want to initialize a 2d array which its elements number in each row is not constant.

Something like: int a[][] = {{1,2,3}, {4,5}};

How do I achieve that?

Lior
  • 5,841
  • 9
  • 32
  • 46

1 Answers1

1

You can't achieve this using a raw 2d array. What you can do is use a vector that holds other vectors. For instance in your case you could do:

std::vector<std::vector<int>> a = { {1, 2, 3}, {4, 5} };
Veritas
  • 2,150
  • 3
  • 16
  • 40