简体   繁体   English

在某些元素上提供带有迭代器的整数矩阵

[英]Providing an integer matrix with an iterator over certain elements

I'm trying to find a solution for the following problem: 我正在尝试寻找以下问题的解决方案:

Suppose we have implemented an abstract data type "integer matrix" (assume that integers are stored in rows). 假设我们已经实现了抽象数据类型“整数矩阵”(假设整数存储在行中)。 We need to define an iterator to iterate over the matrix by columns and only over the even elements of the matrix. 我们需要定义一个迭代器,以按列遍历矩阵,并且仅遍历矩阵的偶数元素。

Executing the folling code: 执行以下代码:

 Matrix M; ... Matrix::iterator it; for(it = M.begin(); it != M.end();++it) cout << *it 

on the matrix: 在矩阵上:

 5 4 3 2 1 2 9 0 2 8 9 1 

should produce 2 8 4 0 2 2 应该产生2 8 4 0 2 2

I face several problems to do it. 我要解决这个问题。 First, I would like to avoid providing the class iterator with much information about the matrix it is iterating over. 首先,我想避免为class iterator提供有关要迭代的矩阵的大量信息。 Is it inevitable to provide the number of rows and columns of the matrix? 提供矩阵的行数和列数是否不可避免? Second, the solution depends a lot on the internal representation of the matrix so I'd say I should implement a basic iterator to iterate row by row... 其次,解决方案在很大程度上取决于矩阵的内部表示,因此我想我应该实现一个基本的迭代器来逐行迭代...

What do you think is the best solution to this problem? 您认为该问题的最佳解决方案是什么? I would just need advice about the representation of the matrix and the iterator and the functions begin,end,operator*,operator++ and operator == 我只需要有关矩阵和迭代器的表示的建议,并且函数开始,结束,运算符*,运算符++和运算符==

Without guessing at which of the operations that you haven't specified need to be especially efficient, it appears a flat linear storage of the elements would be best. 在不猜测您未指定的哪些操作需要特别高效的情况下,似乎最好是元素的扁平线性存储。 By calling it Matrix::iterator (and using begin() and end() as you have shown) you have implied this is the ordinary iterator used for this Matrix class (not some weird extra iterator). 通过将其称为Matrix :: iterator(并使用如您所示的begin()和end()),您暗示这是用于Matrix类的普通迭代器(而不是一些奇怪的额外迭代器)。 So it is worth a little extra content in the matrix to keep the iterator light weight. 因此,保持矩阵的轻量化值得在矩阵中添加一些额外的内容。 You can make the iterator's data content be only a pointer, so in an optimized compile the iterator itself is only a pointer. 您可以使迭代器的数据内容仅是一个指针,因此在优化的编译中,迭代器本身仅是一个指针。 It doesn't need to know row count or column count or limit. 它不需要知道行数或列数或限制。

To do that, you need to allocate and initialize a guard position at the limit of the matrix data and it must hold an even number and end() must be a pointer to it (wrapped in the iterator class). 为此,您需要在矩阵数据的极限处分配并初始化一个保护位置,并且该保护位置必须包含偶数,并且end()必须是指向该位置的指针(包装在迭代器类中)。 Then begin() can simply find the first even number and the iterator's ++ can simply find the next even number. 然后begin()可以简单地找到第一个偶数,而迭代器的++可以简单地找到下一个偶数。

To support other operations that we assume must exist (but you haven't asked about) the Matrix itself must know number of columns. 为了支持我们假设必须存在的其他操作(但您尚未询问),Matrix本身必须知道列数。 For end() and other operations, the Matrix must know the total size (represented as number of rows or number of elements or limit address are anything else easily translated to limit address). 对于end()和其他操作,矩阵必须知道总大小(表示为行数或元素数或限制地址是可以轻松转换为限制地址的任何其他内容)。 But that extra info is better kept in the Matrix and not duplicated into the iterator. 但是,这些额外的信息最好保留在Matrix中,而不要复制到迭代器中。

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

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