How do I declare array of array but with different size?

Pages: 12
I am a beginner.
I searched for hours an got confused about how declaring this:
{ {1, 2}, {1, 2, 3} }
Both outer and inner array had known size.
How do I achieve this?
You can do this only dynamically allocating memory.
For example

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
 
int main()
{
    int * a[2] = { new int[2] { 1, 2 }, new int[3] { 1, 2, 3 } };
    
    for ( size_t i = 0; i < 2; i++ ) std::cout << a[0][i] << ' ';
    std::cout << std::endl;
    for ( size_t i = 0; i < 3; i++ ) std::cout << a[1][i] << ' ';
    std::cout << std::endl;
}



It is more suitable to use std::vector for this purpose because it contains the value of the number of elements.
and doesn't leak memory
Thank you!
Could you show me the vector-way too?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <vector>
#include <numeric>
 
int main()
{
    int * a[2] = { new int[2] { 1, 2 }, new int[3] { 1, 2, 3 } };
    
    for ( size_t i = 0; i < 2; i++ ) std::cout << a[0][i] << ' ';
    std::cout << std::endl;
    for ( size_t i = 0; i < 3; i++ ) std::cout << a[1][i] << ' ';
    std::cout << std::endl;
    
    std::cout << std::endl;
    
    const size_t N = 10;
    std::vector<std::vector<int>> v( N );
 
    size_t i = 1;
    for ( std::vector<int> &inner : v )
    {
        inner.resize( i );
        std::iota( inner.begin(), inner.end(), 1 );
        ++i;
    }
    
    for ( const std::vector<int> &inner : v )
    {
        for ( int x : inner ) std::cout << x << ' ';
        std::cout << std::endl;
    }
}



The output is

1 2 
1 2 3 

1 
1 2 
1 2 3 
1 2 3 4 
1 2 3 4 5 
1 2 3 4 5 6 
1 2 3 4 5 6 7 
1 2 3 4 5 6 7 8 
1 2 3 4 5 6 7 8 9 
1 2 3 4 5 6 7 8 9 10
Last edited on
I copied your code but I got an error at this line:

int * a[2] = { new int[2] { 1, 2 }, new int[3] { 1, 2, 3 } };

It highlighted "{" from "{1, 2}", and says "Error: Expected a '}'".

What am I doing wrong?
You are using a wrong compiler.:)

Test the code on-line at www.ideone.com selecting C++ 11.

http://ideone.com/XI7kYS
Last edited on
How do I get the right compiler than?
I am using VS Express 2012.
I wonder. I think that MS VC++ 2012 supports braced-list initialization. Maybe you a made a typo when you were coping the code?

At least if you want to see how to work with vector you can remove the part of the code that deals with the array.
Last edited on
Double checked, still happens at only that line:\

Emmm.. It said std has no member "vector".
Last edited on
It seems that you are unable simply to copy and paste the code.:)

If you are using a MS VC++ console application then the first header should be

#include "stdafx.h"
Last edited on
Since I don't want stdafx(I want stdio)
I created an empty project instead.
and tested this code, it works.

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include "stdio.h"

using namespace std;

int main (int argc, char* argv)
{
	cout << "Hello World!" << endl;
	cin.get();
	return 0;
}
Then copy and paste my code excluding the part that deals with the array. What is the problem?
I did. and it shows the error above. Am I missing something?
You are unable to do the simplest operation as copy and paste. I am sorry but I do not see any sense to continue the discussion.
Ok. Thanks anyway, at least you answered my question. I'll figure out the rest myself:)
I am a beginner.
I searched for hours an got confused about how declaring this:
{ {1, 2}, {1, 2, 3} }
Both outer and inner array had known size.
How do I achieve this?
Coda

The original post is unclear about when size of the array was known...

If the array's various sizes are known and fixed size at compile time, then the easiest solution would be to use a 2 by 3 array and just ignore one of the elements. Unless you use a C++ vector, it's your responsibily to keep track of the rows, anyway. Note that while this solution wastes an array element, it will take up (slghtly) less memory than the more flexible pointer based approach.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

using namespace std;

int main()
{
    int a[2][3] = { {1, 2 /*,0*/} // this extra elem. 0-ed by the compiler 
                  , {1, 2, 3} };

    const size_t elems_for_i[2] = {2, 3};

    for ( size_t i = 0; i < 2; ++i )
        for ( size_t j = 0; j < elems_for_i[i]; ++j )
            std::cout << "a[" << i << "][" << j << "] = "
                      << a[i][j] << '\n';

    return 0;
}


You can also do the following, if you don't mind a bit more typing:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
using namespace std;

int main()
{
    int a_0[] = {1, 2};
    int a_1[] = {1, 2, 3};

    int* a[2] = {a_0, a_1}; // int* array

    const size_t elems_for_i[2] = {2, 3};

    for ( size_t i = 0; i < 2; ++i )
        for ( size_t j = 0; j < elems_for_i[i]; ++j )
            std::cout << "a[" << i << "][" << j << "] = "
                      << a[i][j] << '\n';

    return 0;
}


Andy
Last edited on
What if I have very large array that contains 10000 elements and vary from range 1 to 100?
Which one is better if I use 10000 pointers or declaring a 10000 by 100 array and ignoring some of the elements?
With a large array, the wasted space could get quite large. So I guess I would go for either the pointer based or vector base solution or, potentially, the int array/int* array.

A more complete answer would depend on what you're using the array for.

Andy
Last edited on
I am using it for matching and replacing bytes.
what I am trying to do is declare a large table of byte pattern.
Then loop through the pattern table to match and replace an input stream.
Is the pointer a better idea?
Pages: 12