0

I m trying to create a vector with not known number of dimensions at compile time.

i found here some topics talking on the same subject but didn't answered my case like Create n-dimensional vector with given sizes and C++: generate multidimensional vector of unknown depth

i could extract one working solution (compiles without errors) from first link above but i couldn't implement it.

template <typename T, int n>
struct NDVector {
    typedef std::vector<typename NDVector<T, n - 1>::type> type;
};

template <typename T>
struct NDVector<T, 0> {
    typedef T type;
};

template <typename T>
std::vector<T> make_vector_(std::size_t size) {
    return std::vector<T>(size);
}


template <typename T, typename... Args>
typename NDVector<T, sizeof...(Args) + 1>::type make_vector_(std::size_t first, Args... sizes) {
    typedef typename NDVector<T, sizeof...(Args) + 1>::type Result;
    return Result(first, make_vector_<T>(sizes...));
}

and create my vector variable like below

NDVector<int,4> myVector;

This is the only reasonable solution, but i couldn't push, erase, clear .... from myVector. also cannot use sub-scripting notation (myVector[x]). the second problem here that i couldn't use variable int instead of a constant number in the declaration (change number '4' with variable).

My goal is to be able to declare the vector like that:

unsigned n;
NDVector<int,n> myVector;

and also being able to use the 'myVector' variable like normal vectors

  • Sorry, C++ does not work this way. The type of every object must be known at compile time. This is fundamental to C++. – Sam Varshavchik Apr 26 '20 at 16:45
  • 1
    *I m trying to create a vector with not known number of dimensions at compile time.* -- Sounds like an [XY problem](http://xyproblem.info/) – PaulMcKenzie Apr 26 '20 at 16:47
  • Are you trying to create a vector of vectors here ? – Youssef Sbai idrissi Apr 26 '20 at 16:50
  • A template argument must be a compile time constant but I think something like can be achieved without templates using polymorphism instead. – Thomas Sablik Apr 26 '20 at 16:57
  • maybe [A Class Template for N-Dimensional Generic Resizable Arrays](https://www.drdobbs.com/a-class-template-for-n-dimensional-gener/184401319) is of interest – Thomas Apr 26 '20 at 17:07
  • @SamVarshavchik i know the type should be know at compile time but i want the number of dimension is unknown somthing like vector> myVector. – Mohamed Elleuch Apr 26 '20 at 17:35
  • @YoussefSbaiidrissi no. vector of vector is 2D dimensions and vector of vector of vector is 3D .... i want N dimension – Mohamed Elleuch Apr 26 '20 at 17:35
  • @ThomasSablik it will be more helpful if u give a small piece of code as an example – Mohamed Elleuch Apr 26 '20 at 17:36
  • @Thomas thnx for answer but the link that u gave is like the example that i said Array A5; // Five-dimensional array of ints! Array A1; // One-dimensional array of floats? the array size is already known (5 for a5 and 1 for a1) but i m looking for something like: int n; n = getTheValueOfN(); Array A_N; – Mohamed Elleuch Apr 26 '20 at 17:40
  • The number of dimensions is a part of a type. `vector` and `vector>` are two different types. You must know the type at compile time. C++ does not work this way. You cannot have an object with an unknown number of dimensions, in C++. – Sam Varshavchik Apr 26 '20 at 17:46
  • `Array` can't be done in c++, olnly something like `Array(n)` and you would need to know the size of each dimension, so prob. more like ``Array( d1,d2,..,dn )`. – Thomas Apr 26 '20 at 17:54
  • found [lite array - A C++ Multidimensional Array Library](https://www.cs.cornell.edu/~saeed/lite/html/group__array.html), that seems pretty close – Thomas Apr 26 '20 at 18:00

1 Answers1

2

That’s actually way simpler that a compile-time solution.

Your class needs to store two vectors: one for bounds (as their count is unknown at compile time) and another for actual data (so its size is bounds[0]*...*bounds[dim-1]).

Then it needs the subscription operator, or get/set functions. As the dimensionality is not know yet, they need to accept an array of indices, not a fixed count of arguments. Vector is perfectly suitable. Given that, it can calculate the index in the internal array, and return a reference to that particular element.

Then you may try to handle all the sort of resize pain. Or just don’t promise to keep any data on resize.

numzero
  • 2,009
  • 6
  • 6