views:

111

answers:

6

As suggested here I fixed my 2D array of numbers to make it work with Vector class.

Header file:

#include <vector>

typedef std::vector<int> Array;
typedef std::vector<Array> TwoDArray;

And here is how it's used:

TwoDArray Arr2D; 

// Add rows
for (int i = 0; i < numRows; ++i) {
    Arr2D.push_back(Array());
}

// Fill in test data
for (int i = 0; i < numRows; i++) {    
    for (int j = 0; j < numCols; j++) {
        Arr2D[i].push_back(ofRandom(0, 10));           
    }
}

// Make sure the data is there
for (int i = 0; i < numRows; i++) {    
    for (int j = 0; j < numCols; j++) {
        std::cout << Arr2D[i][j] << ' ';
    }
std::cout << '\n';
}

My question is, how can I do the same for custom objects instead of int numbers? I've tried a changing int by MyObject and using push_back(new MyObject()); but it doesn't work properly when I try to access it's functions.

Thank you in advance.

A: 

you need to change

typedef std::vector<int> Array;
typedef std::vector<Array> TwoDArray;

to

typedef std::vector<MyObject> Array;
typedef std::vector<Array> TwoDArray;
mihirpmehta
A: 

In outline:

struct MyObject {};

typedef std::vector<MyObject> Array;
typedef std::vector<Array> TwoDArray;

// stuff

Arr2D[i].push_back( MyObject() );  

Note that to store things in a vector they must be copyable and assignable.

anon
+1  A: 

new MyObject() will return a pointer to the newly created an instance of class MyObject. If you have created a vector<MyObject> then you need to do something like push_back(MyObject()).

Naveen
+1 easy mistake to make if you come from say a Java background.
Mark B
A: 

Try this:

template<class T>
struct Array2D {
    vector<vector<T>> Array;
};

now you can declare it to any object like so

Array2D<MyObject>::Array array;

or even

Array2D<MyObject*>::Array array;

and carry with your day as normal....

Be aware that when you assign an object to a stl container it should implement, a default no-arg constructor, a copy constructor and overload operator= (const and non-const), otherwise you should insert a pointer to the object.

Adrian Regan
A: 

If I were you, I would use array of pointers instead of object themselves (but you will have to take care of deallocation before doing your "clear()").

typedef std::vector<std::vector<myObject* > > 2DArray;

2DArray A;
for (int i=0; i<numRows;++i) A.push_back(std::vector<myObject* >(numColumns));

myObject* M = new myObject(...);
A[row][column] = M;
Elenaher
A: 

Since you are searching for the best design approach, why not follow the advice of C++ FAQ Lite and make your array dense (rather than ragged). You're not expecting to have to handle rows of different size, are you?

#include <iostream>
#include <random>
#include <vector>

template<typename T>
class TwoDArray {
        std::vector<T> data;
        const size_t cols;
public:
        TwoDArray(size_t R, size_t C) : data(R*C), cols(C) {}
        T operator()(size_t r, size_t c) const { return data[cols*r+c]; }
        T& operator()(size_t r, size_t c) { return data[cols*r+c]; }
};

int main()
{
        // Make it
        const size_t numRows = 10;
        const size_t numCols = 10;
        TwoDArray<int> Arr2D(numRows, numCols);

        // Fill in test data
        std::random_device rd;
        std::mt19937 eng(rd());
        std::uniform_int_distribution<> unif(0,9);
        for (size_t i=0; i<numRows; ++i)
           for (size_t j=0; j<numCols; ++j)
                Arr2D(i,j) = unif(eng);

        // Make sure the data is there
        for (size_t i = 0; i < numRows; i++) {
            for (size_t j = 0; j < numCols; j++) {
                 std::cout << Arr2D(i,j) << ' ';
            }
            std::cout << '\n';
        }

}

Or you could even make size part of type, just like std::array.

Cubbi