[英]How to overload subscript operator for multidimensional vectors?
This is my current Matrix class: 这是我当前的Matrix类:
template<class T>
class Matrix
{
public:
Matrix() { }
Matrix(int z, int x, int y)
{
matrix.resize(z);
for (unsigned int i = 0; i < matrix.size(); i++)
matrix[i].resize(x);
for (unsigned int i = 0; i < matrix.size(); i++)
{
for (unsigned int j = 0; j < matrix[i].size(); j++)
matrix[i][j].resize(y);
}
Fill(0);
}
int dim1() { return matrix.size(); }
int dim2() { return matrix[0].size(); }
int dim3() { return matrix[0][0].size(); }
void Fill(int n);
bool Set(int z, int x, int y, T value);
class Row
{
std::vector<std::vector<T>> m_row;
public:
Row(std::vector<std::vector<T>> row) : m_row(row) { }
class Column
{
std::vector<T> m_column;
public:
Column(std::vector<T> column) : m_column(column) { }
T& operator [] (int index) { return this->m_column[index]; }
};
Column operator [] (int index) { return Column(m_row[index]); }
};
Row operator [] (int index) { return Row(matrix[index]); }
private:
std::vector<std::vector<std::vector<T>>> matrix;
};
template<class T>
void Matrix<T>::Fill(int n)
{
for (unsigned int i = 0; i < matrix.size(); i++)
for (unsigned int j = 0; j < matrix[0].size(); j++)
for (unsigned int k = 0; k < matrix[0][0].size(); k++)
matrix[i][j][k] = n;
}
template<class T>
bool Matrix<T>::Set(int z, int x, int y, T value)
{
if (z < matrix.size() && x < matrix[0].size() && y < matrix[0][0].size())
{
matrix[z][x][y] = value;
return true;
}
else
return false;
}
everything works, except the one line commented out in the code below 一切正常,除了下面的代码中注释掉的一行
Matrix<int> m(3, 10, 20);
m.Set(2, 4, 10, 42);
// m[2][4][10] = 42;
std::cout << "base layer: " << m.dim1() << std::endl;
std::cout << "layer x: " << m.dim2() << std::endl;
std::cout << "layer y: " << m.dim3() << std::endl;
std::cout << "\nm[2][4][10] = " << m[2][4][10] << std::endl;
when I do "m[2][4][10] = 42", instead of the "Set(x, x, x, x)" function, the "cout << m[2][4][10]" returns 0 instead of 42. It just doesn't make sense to me and I would really like to use the subscripting to set values. 当我执行“ m [2] [4] [10] = 42”而不是“ Set(x,x,x,x)”函数时,“ cout << m [2] [4] [10] ”返回0而不是42。对我来说这没有意义,我真的很想使用下标设置值。
Edit : I changed the question title to make a bit more sense. 编辑 :我更改了问题标题,使它更有意义。
Row operator [] (int index) { return Row(matrix[index]); }
The return type here designates a function that returns by-value . 这里的返回类型指定一个返回按值的函数。 Thus, a copy of matrix[index]
is returned and any subsequent operations done on the return value only affect the copy but not the original object. 因此,将返回matrix[index]
的副本,并且对返回值执行的任何后续操作只会影响副本,而不会影响原始对象。
Invariably, the solution is to return an lvalue-reference. 解决方案总是返回一个左值引用。 But that still won't help because of how you're returning the index. 但这仍然无济于事,因为您如何返回索引。 Row(...)
constructs a temporary instance of Row
which is separate from the Row
object you should be returning - it is actually a copy of it. Row(...)
构造一个Row
的临时实例,该实例与您应返回的Row
对象是分开的-实际上是它的副本。 There's no need to use this syntax, use return directly: 无需使用此语法,直接使用return:
Row& operator [] (int index) { return matrix[index]; }
Note that this not only applies to this function but the one that returns Column
as well. 请注意,这不仅适用于此函数,还适用于返回Column
函数。
I looked at my code again today and realized all I need is the reference to go both ways. 我今天再次查看了我的代码,意识到我所需要的只是双向引用。 So I changed the Row and the Column variables to references, tested it and it worked. 因此,我将“行”和“列”变量更改为引用,对其进行了测试并正常工作。
class Row
{
std::vector<std::vector<T>>& m_row;
public:
Row(std::vector<std::vector<T>>& row) : m_row(row) { }
class Column
{
std::vector<T>& m_column;
public:
Column(std::vector<T>& column) : m_column(column) { }
T& operator [] (int index) { return this->m_column[index]; }
};
Column operator [] (int index) { return Column(m_row[index]); }
};
Row operator [] (int index) { return Row(matrix[index]); }
I didn't want to use pointers to avoid memory leeks, which was my initial solution and probably a bad one. 我不想使用指针来避免内存溢出,这是我最初的解决方案,可能是一个糟糕的解决方案。 I think this should be a good subscript overload. 我认为这应该是一个很好的下标重载。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.