我正在尝试学习如何创建一个正确初始化向量矢量以实现矩阵的类。 下面的代码不起作用,在运行构造函数后,向量的大小为0.程序打印0并尝试访问它的元素会导致错误。
我的第一个问题是代码有什么问题以及如何修复它。 我的第二个问题是,是否有更好的方法来创建一个类来使用STL中的向量或类似对象动态地实现矩阵。码:
class Matrix{ std::vector<std::vector<int> > Mat; public: Matrix(int n, int m); void set(int a, int b, int Value); int get(int a, int b); void size(); }; Matrix::Matrix(int n, int m){ std::vector<std::vector<int> > Mat(n, vector<int>(m)); } void Matrix::size(){ std::cout << std::endl << Mat.size() << std::endl; } int Matrix::get(int a, int b){ return Mat[a][b]; } void Matrix::set(int a, int b, int Value){ Mat[a][b]=Value; } int main(int argc, char** argv) { Matrix M(10,10); M.size(); return 0; }I'm trying to learn how to create a class which properly initializes a vectors of vectors to implement a matrix. The code below doesn't work, after running the constructor the vector has size 0. The program prints 0 and attempts to access elements of it result in errors.
My first question is what is wrong with the code and how to fix it. My second question is if there is a better approach to creating a class to implement a matrix dynamically using vectors or similar objects from the STL.Code:
class Matrix{ std::vector<std::vector<int> > Mat; public: Matrix(int n, int m); void set(int a, int b, int Value); int get(int a, int b); void size(); }; Matrix::Matrix(int n, int m){ std::vector<std::vector<int> > Mat(n, vector<int>(m)); } void Matrix::size(){ std::cout << std::endl << Mat.size() << std::endl; } int Matrix::get(int a, int b){ return Mat[a][b]; } void Matrix::set(int a, int b, int Value){ Mat[a][b]=Value; } int main(int argc, char** argv) { Matrix M(10,10); M.size(); return 0; }最满意答案
1)当前代码
问题是当你输入构造函数的主体时已经构造了Mat 。 你所做的只是重新定义一个本地Mat ,它隐藏了具有相同名称的成员,并在退出构造函数后立即消失。
试试这个:
Matrix::Matrix(int n, int m) : Mat(n, vector<int>(m)) { }2)有更好的方法吗?
这一切都取决于你打算做什么,你的约束是什么,以及有什么权衡取舍:
如果矩阵的大小并不总是在编译时定义,那么这种实现相当不错,并且代码非常易读。
如果你有许多相当小的向量,一种替代方法可能是将矩阵的内部表示展平为一维向量。 你需要一些向量,但必须计算getter和setter的flatened索引。 如果您的Matrix类提供了大量的矩阵运算,会使代码的可读性降低(即Mat[n][m]与Mat[n*width+m] )
如果矩阵的大小是在编译时确定的(例如,你只使用2D或3D矩阵),那么使用std::array而不是std::vector有意义的:然后编译器可以使用已知的大小来生成更快的代码。
1) Current code
The problem is that Mat is already constructed when you enter the body of your constructor. What you do is just redefine a local Mat which hides the member having the same name and which vanishes as soon as you exit the constructor.
Try this instead:
Matrix::Matrix(int n, int m) : Mat(n, vector<int>(m)) { }2)Are there better approaches ?
it all depend on what you intend to do, whar atre your constraints, and what are the trade-offs:
If the size of you matrixes is not always defined at compile time, this kind of implementation is fairly good, and the code very readable.
If you have many rather small vectors, one alternative could be to flatten the internal representation of the matrix to a unidimensional vector. You'd spare some vectors, but have to calculate the flatened index for the getter and the setter. If your Matrix class would provide a lot of matrix operations, would make the code less readable (i.e. Mat[n][m] vs. Mat[n*width+m])
If the size of your matrix is determined at compile time (e.g. you use only 2D or 3D matrixes), it could make sense to use std::array instead of std::vector: the compiler could then make use of the known sizes to generate faster code.
更多推荐
发布评论