1

Is it possible in c++ to create a type of variable that can hold an array with any dimensions? I mean a variable that can store a 1, 2, 3 ... dimensional array.

I guess that it can be made by templates but I could not figure it out how. I would really appreciate if anyone could help.

AdamVarga
  • 39
  • 4
  • 2
    Could you possibly show an example of the kind of code that you would intend to write with this? – Xirema Apr 22 '19 at 14:30
  • 2
    what about `(void*)`? – Michael Feldmeier Apr 22 '19 at 14:31
  • "*Is it possible...?*" The answer is almost always "*Yes,*" but something tells me that won't help you. Can you give us a little more information on what you're trying to do, what you've tried and where it went wrong so that we can give you useful answers? – scohe001 Apr 22 '19 at 14:32
  • Look here https://stackoverflow.com/questions/55285352/how-dynamically-filled-multidimensional-vector-without-a-priori-known-its-dimens – user2807083 Apr 22 '19 at 14:32
  • I want to create a function that can multiply arrays and I will need to use it for a lot of different dimensions and I just wondered if I could do it without writing it a separate one for all dimensions. – AdamVarga Apr 22 '19 at 14:36
  • Create a class that would do that in runtime. One possible implementation is to reuse Microsoft's SAFEARRAY if using Windows. Edit: If it's for multiplication then you can use well known Matrix classes. – Michael Chourdakis Apr 22 '19 at 14:37
  • 1
    @AdamVarga what do you mean by *multiply arrays*? Are you talking about something like dot product or matrix multiplication? – Guillaume Racicot Apr 22 '19 at 14:42
  • I wanna do something like matrix multiplication but with any array dimension – AdamVarga Apr 22 '19 at 14:44
  • related: https://stackoverflow.com/questions/43358369/c-n-nested-vectors-at-runtime – NathanOliver Apr 22 '19 at 14:51

2 Answers2

3

It sounds like you want to "create a function that can multiply arrays" that will be used "for a lot of different dimensions."

I would deal with this just like I would deal with a vector output operator: use templates with recursion!

If I wanted to make a function to sum all of the numbers in two vector's when I add them, I could do:

template <typename T>
int operator+(std::vector<T> v1, std::vector<T> v2) {
    if(v1.size() != v2.size()) { throw; } //for simplicity
    int sum = 0;
    for(size_t x = 0; x < v1.size(); x++) {
        sum += v1.at(x) + v2.at(x);
    }
    return sum;
}

Note that the magic here is in the line

sum += v1.at(x) + v2.at(x);

If v1.at(x) and v2.at(x) are std::vector's, we'll just recursively call this function again. But if they're int's, we'll add them into sum and move on.

You can see this in action here: ideone

You could do something similar for your array multiplication. Break the problem down into smaller pieces so you can use recursion and let the templates handle the rest!

scohe001
  • 15,110
  • 2
  • 31
  • 51
2

Do you ever try dynamic memory? In the case below, it creates a two-dimensional array.

http://www.cplusplus.com/doc/tutorial/dynamic/

If you want to have a three-dimensional array, maybe you can try to define a two-dimensional array in step two of the above example.

For four-dimensional array, third-dimensional array in step 2.

Or, you can keep using pointer.

Hong
  • 526
  • 6
  • 21