English 中文(简体)
c++中的矩阵级?
原标题:Matrix class in c++?
  • 时间:2012-04-20 16:40:06
  •  标签:
  • c++
  • matrix

我正试图撰写一个矩阵,我正在使用一种主要方法加以测试。 它根本没有工作。

我不理解的是,大小(所有RowValues的大小按两倍的大小)是零!

I ve been writing some debug prints but it isn t helping... I m really, really new with C++ so ANY and all help / advice will be appreciated.

 1 #include "matrix.h"
 2 #include <iostream>
 3 #include <sstream>
 4 
 5 Matrix::Matrix(){
 6 };
 7 
 8 Matrix::Matrix(int rows, int columns, double allRowValues [] ){
 9     this->rows = rows;
10     this->columns = columns;
11     this->matrixValues = new double[rows*columns];
13     std::cout <<"ALL ROW VALUES" <<std::endl;
14     std::cout<<"*****" <<std::endl;
15     std::cout << sizeof (allRowValues) << std::endl;
16     std::cout<<"*****" <<std::endl;
17     std::cout << sizeof(allRowValues[0]) << std::endl;
18     std::cout<<"*****" <<std::endl;
19     int size = sizeof(allRowValues)/sizeof(double);
20     int numberOfValues = rows * columns;
21     int currentIndex = 0;
22     for (int i = 0; i < numberOfValues; i++){
23             std::cout<< "MATRIX CONSTRUCTOR
";
24             std::cout<<allRowValues <<std::endl;
25             std::cout<<"-----"<<std::endl;
26             std::cout<<index << std::endl;
27             std::cout<<"-----"<<std::endl;
28             std::cout<<size << std::endl;
29             std::cout<<"-----"<<std::endl;
30             if ((allRowValues) && (currentIndex < size)){
31                 std::cout << "ARV " <<std::endl;
32                 std::cout << allRowValues[currentIndex] << std::endl;
33                 this->matrixValues[i]= allRowValues[currentIndex];
34                 std::cout << "MAT " << allRowValues[currentIndex++] << std::endl;
35             }else{
36                 std::cout << "Else
";
37             }
38         }
39         int index=0;
40         for (int j = 0; j < rows; j++){
41             for (int i = 0; i < columns; i++){
42                 std::cout << this->matrixValues[index++];
43             }
44             std::cout<<std::endl;
45         }
46     };
47 
48     Matrix::Matrix(double* rowValues){
49         int sizeRows = sizeof(rowValues)/sizeof(double);
50         //TODO: throw error for all rows must be the same length
51         this->rows = sizeRows;
52         int sizeColumns = sizeof(rowValues[0])/sizeof(double);
53         this->columns = sizeColumns;
54         this->matrixValues = rowValues;
55     };
56 
57     double Matrix::width(){
58         std::cout << "Width
";
59         return this->columns;
60     };
61 
62     double Matrix::height(){
63         std::cout << "Height
";
64         return this->rows;
65     };
66 
67     std::string Matrix::toString(){
68         int numberOfValues = 0;
69         std::cout<<matrixValues[numberOfValues];
70         std::string build_output;
71         std::cout<<matrixValues;
72         for (int i = 0; i < rows; i++){
73             build_output = "[";
74             std::cout << "
";
75             for (int j = 0; j < columns; j++){
76                 std::cout << "VALUE: " <<matrixValues[numberOfValues];
77                 build_output = matrixValues[numberOfValues];
78                 numberOfValues++;
79             }
80             build_output = " ]";
81         }
82         return build_output;
83     }
84 
85     int main (){
86         double values[6] = {1, 2, 3, 4, 5, 6};
87         std::cout <<"Values: 
";
88         Matrix a = Matrix(2, 3, values);
89         std::cout << a.width() << std::endl;
90         std::cout << a.height() << std::endl;
91         std::cout << a.toString();
92         return 1;
93 }
问题回答

。 The expression sizeof(allRowValues)/sizeof(double) 然后计算点大小与double大小之比。 如果两倍增加,结果显然是零。

出于某种原因,其他建筑商也发生了同样的错误(sizeof(rowValues)/sizeof(double)),但这一论点显然是一个要点。 然后,有<条码>(滚动式)/大小(双倍)<>,即一阵的大小与明显是两倍的两倍的大小之比。

似乎认为,鉴于第一点点,“<>条码/代码>的操作者可以mag地知道阵列的大小。 它可以。 也可能在阵列和点之间混淆不清。 页: 1

多数时间,阵列(即T[N]的物体,如double[100])仅指向其第一要素(即T*的阵列,如double*)的大小信息。 因此,如果你打算“绕过一个阵列”,你就永远不会通过一个点。 你们需要以某种方式通过规模信息。

你可以明确地将体积当作一个额外的论据,或者通过另一个标志缓冲末端的点子(单体风格)。 你们还可以提到阵列(这防止 into入点子,从而保护面积信息),并使用模板获取规模信息。

template <std::size_t N, std::size_t M>
void pass_a_2d_array_by_reference(double(&the_array)[N][M]) { // N and M are the sizes
    // do stuff
}

既然你理解这些问题,如果你采用现成的解决办法,你就根本无法解决这些问题:

  • std::vector<std::vector<double>>: it s a very good solution if you don t need contiguous storage. It s also your best shot if you want jagged arrays.
  • boost::multiarray<double, 2>: another very good solution that works just as well with arrays of even more dimensions;
  • there are many others existing solutions, for various needs. Just look around.

如果你坚持采取像样的阵列(我不建议这样做),你也可以采用一种模版解决办法:

template<int size>
Matrix::Matrix(int rows, int columns, double (&allRowValues) [size] ){
...
}

总而言之,我建议从大陆架边取一个开放源矩阵图书馆,例如eigen

您应使用<代码>std:vector<double>&(或以下简称:array)而不是单式阵列double allRowValues[]。 因此,你可以很容易地用<代码>all RowValues.size()确定其面积。

c++ faq-lite:

Reference:
http://en.cppreference.com/w/cpp/container/vector
http://en.cppreference.com/w/cpp/container/array





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签