C++ 2 dimensional array with variable size rows

Viewed 26968

How can you create a 2D array,say, arr[][] with 5 rows and each row has a variable number of columns in it?

possibly arr[5][] with 1st row arr[0][] with 4 columns

2nd row arr[1][] with 5 columns and so on?

I wouldn't mind a STL vector solution but I don't know vectors very well yet.

4 Answers

A way for different row size

#include <iostream>
#include <string>
int main()
{
  int test1[]={1,2,3};
  int test2[]={4,5};

  int *test[]={test1,test2};

  std::cout << test[0][1];//2
  std::cout << test[1][1];//5
}
Related