C ++ - 基于矢量的二维对象数组

时间:2022-10-10 21:18:09

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

正如我在这里建议的那样,我修复了我的2D数组,使其与Vector类一起使用。

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.

我的问题是,我如何为自定义对象而不是int数字做同样的事情?我已尝试通过MyObject更改int并使用push_back(new MyObject());但是当我尝试访问它的功能时,它无法正常工作。

Thank you in advance.

先谢谢你。

6 个解决方案

#1


1  

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()).

new MyObject()将返回一个指向新创建的类MyObject实例的指针。如果您已经创建了一个向量 ,那么您需要执行类似push_back(MyObject())的操作。

#2


1  

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.

请注意,当您将对象分配给stl容器时,它应该实现,默认的无参数构造函数,复制构造函数和重载operator =(const和非const),否则您应该插入指向该对象的指针。

#3


1  

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()").

如果我是你,我会使用指针数组而不是对象本身(但是你必须在做“clear()”之前处理deallocation)。

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;

#4


1  

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?

由于您正在寻找最佳设计方法,为什么不遵循C ++ FAQ Lite的建议并使您的阵列密集(而不是粗糙)。您不期望必须处理不同大小的行,是吗?

#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.

或者你甚至可以将size作为类型的一部分,就像std :: array一样。

#5


0  

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;

#6


0  

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.

请注意,要将内容存储在向量中,它们必须是可复制和可分配的。

#1


1  

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()).

new MyObject()将返回一个指向新创建的类MyObject实例的指针。如果您已经创建了一个向量 ,那么您需要执行类似push_back(MyObject())的操作。

#2


1  

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.

请注意,当您将对象分配给stl容器时,它应该实现,默认的无参数构造函数,复制构造函数和重载operator =(const和非const),否则您应该插入指向该对象的指针。

#3


1  

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()").

如果我是你,我会使用指针数组而不是对象本身(但是你必须在做“clear()”之前处理deallocation)。

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;

#4


1  

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?

由于您正在寻找最佳设计方法,为什么不遵循C ++ FAQ Lite的建议并使您的阵列密集(而不是粗糙)。您不期望必须处理不同大小的行,是吗?

#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.

或者你甚至可以将size作为类型的一部分,就像std :: array一样。

#5


0  

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;

#6


0  

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.

请注意,要将内容存储在向量中,它们必须是可复制和可分配的。