1

I am trying to implement a dynamic array with new because the standard std libraries do not. here is my code.

template <class T1, class T2, class T3>
auto auto_vector(T1& _Size, T2& _Size2, T3& _Size3) {
    auto result = new int64_t **[_Size3];
    for (ptrdiff_t i = 0; i < _Size3; ++i) {
        result[i] = new int64_t *[_Size2];
        for (ptrdiff_t k = 0; k < _Size2; ++k) {
            result[i][k] = new int64_t [_Size]{};
        }
    }
    return result;
}

this is how i delete my array

template <class T1, class T2, class T3, class T4>
void del_vector(T4& _Del, T1& _Size, T2& _Size2, T3& _Size3) {
    for (ptrdiff_t i = 0; i < _Size3; ++i) {
        for (ptrdiff_t k = 0; k < _Size2; ++k) {
            delete _Del[i][k];
        }
        delete _Del[i];
    }
    delete _Del;
}

I can use it to create a multidimensional array with three tabs. like so

auto tor3 = auto_vector(_Size, _Size2, _Size3);

But I also want to create other arrays with two tabs or four, or all of a sudden I need to create a 10x multi-dimensional array. If suddenly I lose my mind like so

int _Size, _Size2, _Size3, _Size4, _Size5; 
_Size = _Size2 = _Size3 =_Size4 = _Size5 = 4;


auto tor2 = auto_vector(_Size, _Size2);

auto tor1 = auto_vector(_Size);

auto tor10 = auto_vector(_Size,_Size2,_Size3,_Size4,_Size5);

How can I make one function from my function that can do all this, and is it possible to do this?

cout << " ---------TEST---------- " << endl;
    for (ptrdiff_t i = 0; i < _Size3; ++i) {
        for (ptrdiff_t k = 0; k < _Size2; ++k) {
            for (ptrdiff_t j = 0; j < _Size; ++j) {
                cout << _Tor[i][k][j] << " ";
            }
            cout << endl;
        }
        cout << endl;
    }
    cout << " ---------TEST---------- " << endl;
user
  • 73
  • 6
  • 2
    `std::vector` uses `new`, it's just wrapped in a `std::allocator`. – NathanOliver Dec 11 '19 at 18:33
  • @NathanOliver-ReinstateMonica how then should I create a multidimensional array using a vector. I didn’t succeed for some reason and I created my own :). – user Dec 11 '19 at 18:37
  • Worst case, use `std::vector>`. Best case use something like: https://stackoverflow.com/a/53038618/4342498 – NathanOliver Dec 11 '19 at 18:41
  • You could also use something like https://stackoverflow.com/a/43358434/4342498 – NathanOliver Dec 11 '19 at 18:43
  • there is also [boost::multi_array](https://www.boost.org/doc/libs/1_70_0/libs/multi_array/doc/user.html) – Thomas Dec 11 '19 at 18:55
  • I didn’t really like the multi boots library. I am reading other links, until I realized that the vector cannot work with dynamic data, until I understand it. for example, data int _Size, _Size2, _Size3, _Size4, _Size5; they are not known before compilation, I know the intentions after starting the program. – user Dec 11 '19 at 19:04

1 Answers1

0

You can do this using variadic templates

See this link:

https://eli.thegreenplace.net/2014/variadic-templates-in-c/

See example:

template<class T1>
auto auto_vector(T1&& _Size) {
    return new int64_t[_Size];
}

template <class T1, class... TT>
auto auto_vector(T1&& _Size, TT&&... r)
{
    auto result = new decltype(auto_vector(r...))[_Size];
    for (int64_t i = 0; i < _Size; ++i) {
        result[i] = auto_vector(r...);
    }
    return result;
}

int main()
{
    auto x = auto_vector(2,2,2,2);

    return 0;
}

For the delete you do the same way

Victor Padureanu
  • 604
  • 1
  • 7
  • 12
  • 1
    Whilst this may theoretically answer the question, [it would be preferable](//meta.stackoverflow.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. – NathanOliver Dec 11 '19 at 18:47
  • a little confused I can not for some reason initialize the array with zeros, I do so `return new int64_t[_Size]{};` – user Dec 11 '19 at 19:28
  • Unfortunately, I can’t use this code yet, and don’t know how to initialize the array with zeros. I don’t know how to program like you :) – user Dec 11 '19 at 19:48
  • I figured it out, I was dumb, I had to add in the opposite direction. Thank you very much your example works fine, I will think about how now it is also possible to implement deletions. – user Dec 11 '19 at 20:01