how can i test how many dimensions have the array?

Nov 14, 2019 at 4:05pm
seen these array:
int data[4][5][5][6];
we know that array have 4 Dimensions...
can we calculate how many dimensions 1 array have it?
Nov 14, 2019 at 4:25pm
you don't need to calculate it. Its right there. Why do you want to calculate what you already know at code-writing time? To me this is like

const int x = 1234;
//how can I compute the value of x?

I think std::array will provide this info. I don't know if you can back it out easily of an array or not.
Last edited on Nov 14, 2019 at 4:29pm
Nov 14, 2019 at 4:31pm
just playing with code... but think in these way: if i can calculate the number of array elements:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;

template<class T>
int ArrayCountElements(T &a)
{
    return sizeof(a) / sizeof(a[0]);
}

int main()
{
    int data[4][5][5][6];
    int dataSize = ArrayCountElements(data[0]);
    cout << dataSize;
    return 0;
}

why not calculate the array dimensions!?!
i'm just playing with code and learn more
Nov 14, 2019 at 4:35pm
return sizeof(a) / sizeof(a[0]);

this does not tell you the # of dimensions, it tells you the size of one of the dimensions.
are you asking how to know its 4d instead of 2d, or how big each piece is? The first, I do not see a way to get it. The second you can get as you show here, yes.

you are on the right track if you want to know the length of one dimension.
Last edited on Nov 14, 2019 at 4:36pm
Nov 14, 2019 at 4:37pm
Nov 14, 2019 at 4:40pm
That would do it.
Do you know HOW it works to roll it yourself for educational purposes?! Or is it tapping something at compile time that we can't get at?
Nov 14, 2019 at 4:41pm
if i use on code(for test):
data[0][0][0][0][0]=20;
i will get a compiler error, so i can't test the executed errors and see if is 2D or 3D array
Nov 14, 2019 at 4:45pm
coder777: yes, but the rank use the type name, instead a var name
Nov 14, 2019 at 5:14pm
try this for fun
1
2
3
4
5
6
7
8
9
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
int main()
{
int data[4][5][5][6];
 decltype(data) x; 
cout << typeid(x).name()<<endl;
}


the i is int ... so it tells you # of dimensions and size of each one all in one statment, or you can parse it out cleaner if you dislike the format. A is array, I suppose. I didnt read the docs on it, just guessing. If you want the actual value 4, you have to parse the string a little. Counting the underscores may work, at least for basic types like int. I don't know what it will spew if you have a class.

all that to tell you back what you typed in :P
Last edited on Nov 14, 2019 at 5:25pm
Nov 14, 2019 at 5:30pm
thank you so much to all for all
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 <bits/stdc++.h>
using namespace std;

template<class T>
int ArrayCountElements(T &a)
{
    return sizeof(a) / sizeof(a[0]);
}

template<class T>
int ArrayCountDimentions(T &a)
{
    decltype(&a) x; //must be the adress operator
    string data = typeid(x).name();
    int CountDimention=0;
    for(int i=0; i<data.size();i++)
    {
        if(data[i]=='A')//the 'A' is upper case and the int is 'i'
        {
            CountDimention++;
        }
    }
    return CountDimention;
}

int main()
{
    char data[4][5][5][6];
    cout << ArrayCountDimentions(data);
    return 0;
}

thank you
Nov 14, 2019 at 5:36pm
right, mine worked because the name of an array can collapse to a pointer/address.
you now have 30 lines to figure out what line 29 said.
amusing exercise, at least. I had to look it up, so I learned something as well.
Last edited on Nov 14, 2019 at 5:38pm
Nov 14, 2019 at 5:39pm
creates a 4D array with random values :P
Nov 14, 2019 at 5:41pm
Nov 14, 2019 at 5:48pm
using that way, i can test if the variable is an array:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
template<class T>
bool IsAnArray(T &a)
{
    decltype(&a) x; //must be the adress operator
    string data = typeid(x).name();
    for(int i=0; i<data.size();i++)
    {
        if(data[i]=='A')//'A' is an array indication
        {
            return true;
        }
    }
    return false;
}
Nov 15, 2019 at 3:52am
If you need to know the number of elements in each dimension of the array it looks like std::extent would do it:

http://www.cplusplus.com/reference/type_traits/extent/
Nov 15, 2019 at 10:57pm
i can test if the variable is an array

Yet another link:
https://en.cppreference.com/w/cpp/types/is_array
:-)
Topic archived. No new replies allowed.