简体   繁体   English

C ++中的参考数组/矩阵

[英]Reference Arrays/Matrices in C++

I am new to C++ which is the reason why I'm currently kind of stuck. 我是C ++的新手,这就是为什么我目前陷入困境的原因。 Now here's the Problem: I have a couple of float matrices like this: 现在是问题所在:我有几个这样的浮点矩阵:

static const float matr1[4][8] = {0.0, 0.0, ...};
static const float matr2[7][8] = {0.0, 0.5, ...};

etc. I have a struct like to this one: 等。我有一个像这样的结构:

struct structy{
float matr[][];
int index;
float somevalue;
};

I have a vector of this structy which is created dynamically dependent on other information. 我有这个structy的向量,它是根据其他信息动态创建的。 How can I reference a certain of these declared matrices in my struct variable, given that the first parameter of the struct (rows) varies? 考虑到结构的第一个参数(行)不同,如何在结构变量中引用这些声明的矩阵中的某些矩阵? I need a row of the matrices as a float array later on. 稍后,我需要将矩阵的行作为浮点数组。

Thanks for your help! 谢谢你的帮助!

You should also store the number of columns and the number of rows in structy, so that you know the dimensions of matr at a later point in time. 您还应该按结构存储列数和行数,以便在以后的某个时间知道matr的尺寸。 There is no way to check the length of the 2D array otherwise. 否则,无法检查2D数组的长度。

As for your main question: Are you having troubles accessing individual matrix entries in one of your 2D float arrays (matr)? 关于您的主要问题:在访问2D浮点数组(matr)之一中的单个矩阵条目时遇到麻烦吗?

The only way I have ever seen this done is dynamically: 我见过的唯一方法是动态地:

struct structy{
    float ** matr;

    // Need to add these 2 variables
    int dimensionRow;
    int dimensionCol;

    int index;
    float somevalue;
};

When you place the data into matr , you need to also set dimensionRow , and dimensionCol , as well as dynamically allocate matr prior to filling it, IFF you plan to copy. 当您将数据转换成matr ,你也需要设置dimensionRowdimensionCol ,以及动态分配matr之前,加油吧,IFF计划复制。 If not you can simply set matr to the pointer of one of your pre-defined matrices. 如果没有,您可以简单地将matr设置为您预先定义的矩阵之一的指针。 Either way you will need to also set dimensionRow and dimensionCol . 无论哪种方式,您都需要设置dimensionRowdimensionCol

If you need varying sized matrices, I'd suggest using a vector of vectors. 如果您需要大小不同的矩阵,建议您使用向量向量。 This will save you from the trouble of manually allocating a 2D array and managing its memory. 这样可以避免手动分配2D阵列和管理其内存的麻烦。 One possible implementation: 一种可能的实现:

struct structy {
    std::vector< std::vector<float> > matr;
    int index;
    float somevalue;
};

structy s;
...
s.matr[0][1] = 42.0f;
...

And either grow the vectors on demand using push_back() or grow them beforehand with resize(). 并且可以使用push_back()按需增长矢量,或者事先使用resize()增长矢量。

Now if you just want a reference to an external matrix (pointer to static memory) then you can just declare a pointer to pointer (double pointer): 现在,如果您只想引用外部矩阵(指向静态内存的指针),则可以声明一个指向指针的指针(双指针):

struct structy {
    const float ** matr;
    int index;
    float somevalue;
};

You cannot create a reference (not in the traditional sense) that will refer to different types, and arrays with different lengths are different types. 您不能创建将引用不同类型的引用(不是传统意义上的引用),并且具有不同长度的数组是不同的类型。 However, you can take advantage of the fact that arrays are contiguous, (and so arrays of arrays are contiguous arrays), and create a class that acts as a reference using pointers. 但是,您可以利用以下事实:数组是连续的(因此数组的数组是连续的数组),并创建一个使用指针充当引用的类。

template<typename T>
class matrix_reference
{
public:
    template<size_t R, size_t C>
    void set_reference(T (&arr)[R][C])
    {
        m_start = &arr[0][0];
        m_rows = R;
        m_columns = C;
    }

    T& operator()(size_t r, size_t c)
    {
        return m_start[r * m_columns + c];
    }

    size_t rows() const { return m_rows; }
    size_t columns() const { return m_columns; }
private:
    T* m_start;
    size_t m_rows;
    size_t m_columns;    
};

int main()
{
    matrix_reference<const float> mref;

    mref.set_reference(matr1);
    for (size_t r=0; r<mref.rows(); ++r)
    {
        for (size_t c=0; c<mref.columns; ++c)
            std::cout << mref(r,c) << ' ';
        std::cout << '\n';
    }

    mref.set_reference(matr2);
    for (size_t r=0; r<mref.rows(); ++r)
    {
        for (size_t c=0; c<mref.columns; ++c)
            std::cout << mref(r,c) << ' ';
        std::cout << '\n';
    }    
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM