14

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.

1
  • 1
    Arrays that have variable sizes in one dimension are called jagged arrays. There are numerous other posts on SO about them. They work well for some purposes. I do suggest familiarizing yourself with vectors though.
    – Wug
    Commented Jul 18, 2012 at 6:32

4 Answers 4

17

With C++11, you can do it easily with vectors (line breakes added for readability):

std::vector< std::vector <int > > arr = {
{1,2,3},
{4,5},
{6,7,8,9,0}
};

If you don't have a C++11 compiler, it works the exact same way, but you will not be able to initialize them as easy. You can set elements individually:

std::vector< std::vector <int > > arr;//vector of vectors. Think of each element as of a "row"
std::vector<int> sub;//a temporary "row"
sub.push_back(1);
sub.push_back(2);
arr.push_back(sub);//Adding a "row" to the vector
sub.clear();//Making another one
sub.push_back(1);
sub.push_back(12);
sub.push_back(54);
arr.push_back(sub);//Adding another "row" to the vector

Or you can initialize each "row" with an ordinary array:

std::vector< std::vector <int > > arr;
static const int arr[] = {1,2,3,4};//A "row" as an ordinary array
vector<int> vec (arr, arr + sizeof(arr) / sizeof(arr[0]) ); //Setting a "Row" as a vector
arr.push_back(vec);//Adding the "row" to the vector of vectors. 

It's not exactly possible to do what you want with ordinary arrays, since when you make an array[X][Y], it automaticaly is an X*Y matrix. You could, however, use an array of pointers:

int * array[3];
//also possible: int ** array =  new int*[3]; but don't forget to delete it afterwards.
int sub1[3] = {1,2,3};
int sub2[2] = {1,2};
int sub3[4] = {1,2,3,4};
array[0] = sub1;
array[1] = sub2;
array[2] = sub3;

and access elements with array[X][Y]. However, the vector solution is much better overall.

4
  • I won't downvote you, but I will point out that you targeted a different language than the one he tagged. Your edit makes it somewhat better.
    – Wug
    Commented Jul 18, 2012 at 6:28
  • 8
    @Wug: C++11 is the official standard of the C++ programming language now, it's not a different language. Commented Jul 18, 2012 at 6:38
  • @leftaroundabout: this is one of those technicalities people can and will argue about. It's almost as different from C++ as C++ is from C. Most current C++ compilers don't support it. The point of my comment was to mention that he provided a solution which the asker probably would have no use for.
    – Wug
    Commented Jul 18, 2012 at 6:39
  • 7
    @Wug, it is not really a technicality, the standard clearly says (1.1 - [intro.scope]): This International Standard specifies requirements for implementations of the C++ programming language. The first such requirement is that they implement the language, and so this International Standard also defines C++. However, I would really not like to argue about it. I tried to provide a full answer, though it took some time and a couple of edits. Sometimes it happens that one doesn't consider some things right away, and has to edit an answer later to include additional info. Commented Jul 18, 2012 at 6:49
7

You can do it like this (assuming an array of int elements):

int** arr = new int*[5];
for(size_t i = 0; i < 5; ++i)
{
    arr[i] = new int[4];
}

and this gives you a two-dimensional dynamically allocated array of 5 by 4. You can then use it like this: arr[i][j] = 15;

Do not forget to de-allocate the memory after you are done using the array:

for(size_t i = 0; i < 5; ++i)
{
    delete[] arr[i];
}
delete[] arr;

I would recommend using std::vector, however. You can see the other answers for reference.

0

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

So, 2D arrays is std::vector<std::vector<T>>, where T is type. Also, mb std::array<std::vector<int>, 5>. Or write your own array class.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.