指针类帮助
我正在用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于压缩行矩阵,我使用类似的方法:
然后我可以使用以下方法添加一个条目:
对于每一行,我可以按升序迭代列条目并读出值。
编辑:提出问题的人确实指出他们不能使用 STL。 也许他们可以使用某种映射而不是链表。 否则,我建议使用链表向量,并继续将条目添加到每个列表的末尾。 添加条目完成后,对每个链表进行排序。
For compressed row matrices, I use something like:
I can then add an entry using:
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.
您能详细说明一下您希望operator++() 做什么吗?
例如,要让 LIPointer 的 operator++() 转到下一个右侧元素:
不过,它会在到达末尾时停止。
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:
It stops when it gets to the end, though.