0

Fine, now i want to create my own basic vector and i do:

template <typename T>
class Vector {
    T * vector;
    int size;
public:
    Vector(int pSize = 10) : size(pSize)
    {
        vector = new T[size];
    }
    T& operator[](int i)
    {
        return vector[i];
    }
};

So for a one-dimensional vector everything is fine. I can do:

Vector<int> vectorInt(20);
for (int i = 0; i < 20; i++)
    vectorInt[i] = i;
for (int i = 0; i < 20; i++)
    std::cout << vectorInt[i] << std::endl;

All right, but the problem states when i want to put my vector inside the same vector class, as this:

Vector<Vector<int>> xy(5);
for (int i = 0; i < 5; i++)
{
    for (int j = 0; j < 10; j++)
    {
        xy[i][j] = j;
        std::cout << xy[i][j] << " ";
    }
    std::cout << std::endl;
}

The thing is i cannot initialize to a requested size the insider vector (it defaults to 10 as the constructor says). Only the outsider one to "5".

Is there anyway to initialize the insider vector at declaration?

Lucas
  • 329
  • 1
  • 8
  • 1
    You will have to write more code to be able to do this. [See this question about the analogous initialization for std::vector](https://stackoverflow.com/questions/40887674/initializing-2d-vector-using-only-1-line-c) and implement the same for your template. – Sam Varshavchik May 17 '20 at 00:54
  • @Sam that link doesn't answer my question. I know i have to write more code and i don't know how to do it. – Lucas May 17 '20 at 01:16
  • That link certainly answers your question. It demonstrates exactly how to do what you're trying to do, but with `std::vector`. `std::vector` has an overloaded constructor that takes an optional parameter that specifies the default new value, which gets used to copy-construct each value in the new vector, with an example that passes the default value consisting of `std::vector` of size 2 to the outer constructor of a vector of size 3, thus initializing a 2x3 two-dimensional vector. You simply have to do the same thing: implement an overloaded constructor, plus Rule Of 3 compliance. – Sam Varshavchik May 17 '20 at 01:40
  • @Sam, sorry for the inconvinence, you pointed me in the right direction. It's just not much knowlagde from me. – Lucas May 17 '20 at 02:47

1 Answers1

0

Well, to do my own basic Vector class i did the following: I created a constructor like this one:

Vector(int pSize, T twoDim) : size(pSize)
{
    vector = new T[size];
}

With this constructor the same class takes another instance of Vector<int> and creates the one like the std lib does.

Here is my final code that dinamically initialize a 2d 5x10 matrix:

#include <iostream>
template <typename T>
class Vector {
    T * vector;
    int size;
public:
    Vector(int pSize, T twoDim) : size(pSize)
    {
        vector = new T[size];
    }
    Vector(int pSize = 10) : size(pSize)
    {
        vector = new T[size];
    }
    T& operator[](int i)
    {
        return vector[i];
    }
};

int main()
{
    Vector<Vector<int>> xy(5, Vector<int>(10,0));
    for (int i = 0; i < 5; i++)
    {
        for (int j = 0; j < 10; j++)
        {
            xy[i][j] = j;
            std::cout << xy[i][j] << " ";
        }
        std::cout << std::endl;
    }
    return EXIT_SUCCESS;
}
Lucas
  • 329
  • 1
  • 8