指针类帮助

发布于 2024-07-10 14:30:53 字数 1023 浏览 4 评论 0原文

我正在用 C++ 编写一个稀疏矩阵类,其中每一行和每一列都是我创建的类(恰当地命名为:LinkedList)中的链表数组。

我想编写一个类,它是指向该矩阵中一个单元格的“智能”指针。

在该类中,例如 LIPointer,我将实现一个 ++ 运算符函数,用于在矩阵的链接列表中移动。

有没有一种优雅的方法可以做到这一点,而无需在每次创建 linkedlistPointer 时移动矩阵数组和大小元素的引用?

我不能使用 stl::array 等,因为我必须自己构建它们。

以下是声明:


class LinkedItem 
{ 
private:
    int Column, Row;
    double Value;
    LinkedItem* Right;
    LinkedItem* Down;
public:
...
};

class SparseLinkedMatrix
{
private: //members
    int ColSize;
    int RowSize;
    LinkedItem ** Columns;
    LinkedItem ** Rows;
public: //functions
    SparseLinkedMatrix();
...

};

class LIPointer;
private:
    LinkedItem * CellPointer;
public:
    LIPointer();
        void operator++();//???
...
};

如有任何建议或指导,我们将不胜感激。

更新:需要在整个矩阵上运行。 这就是为什么我认为我需要移动(通过引用)数组和矩阵的大小。 预期的效果是,这将从第一行链接列表中的最后一个单元格到第二行中的第一个单元格。

I'm writing a sparse matrix class in C++ in which every row and column are arrays of linked lists from a class I created (aptly named: LinkedList).

I want to write a class that is a "smart" pointer to one cell in this matrix.

In that class, let say LIPointer, I will implement a ++ operator function for moving in the linked lists of the matrix.

Is there an elegant way of doing this without moving the references of the matrix arrays and sized elements each time I create a linkedlistPointer?

I can't use stl::array etc. because I have to build them myself.

Here are the declarations:


class LinkedItem 
{ 
private:
    int Column, Row;
    double Value;
    LinkedItem* Right;
    LinkedItem* Down;
public:
...
};

class SparseLinkedMatrix
{
private: //members
    int ColSize;
    int RowSize;
    LinkedItem ** Columns;
    LinkedItem ** Rows;
public: //functions
    SparseLinkedMatrix();
...

};

class LIPointer;
private:
    LinkedItem * CellPointer;
public:
    LIPointer();
        void operator++();//???
...
};

Any advice or direction would be appreciated.

Update: It needs to run on the whole matrix. That is why I think I need to move (by reference) the arrays and the size of the matrix. The intended effect is that this would from the last cell in the linked list of the first row to the first cell in the second row.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

森罗 2024-07-17 14:30:53

对于压缩行矩阵,我使用类似的方法:

    std::vector<std::map<size_t, double> > matrix;

然后我可以使用以下方法添加一个条目:

    matrix[row][col] += val;

对于每一行,我可以按升序迭代列条目并读出值。

编辑:提出问题的人确实指出他们不能使用 STL。 也许他们可以使用某种映射而不是链表。 否则,我建议使用链表向量,并继续将条目添加到每个列表的末尾。 添加条目完成后,对每个链表进行排序。

For compressed row matrices, I use something like:

    std::vector<std::map<size_t, double> > matrix;

I can then add an entry using:

    matrix[row][col] += val;

For each row, I can then iterate through the column entries in ascending order and read out the value.

Edit: The person posing the question does point out that they cannot use the STL. Perhaps they can use some kind of map versus a linked list. Otherwise I suggest using a vector of linked lists and keep adding entries to the end of each list. Then do a sort of each linked list when adding entries has been completed.

内心荒芜 2024-07-17 14:30:53

您能详细说明一下您希望operator++() 做什么吗?

例如,要让 LIPointer 的 operator++() 转到下一个右侧元素:

void operator++()
{
    if ( CellPointer != NULL )
        CellPointer = CellPointer->Right;
}

不过,它会在到达末尾时停止。

Can you please elaborate on exactly what you want operator++() to do?

For instance, to have LIPointer's operator++() go to the next right element:

void operator++()
{
    if ( CellPointer != NULL )
        CellPointer = CellPointer->Right;
}

It stops when it gets to the end, though.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文