重载运算符时出现问题 >>对于矩阵类

发布于 2024-10-24 19:29:09 字数 770 浏览 1 评论 0原文

我正在尝试通过重载>>来学习运算符重载 来启用矩阵的基于键盘的输入

Matrix M1;
cin >> M1;

对于矩阵类,通过调用诸如以下的操作符重载部分

istream &operator>>(istream &in, Matrix &m) 
{
    for (int i = 0; i < m.dx; ++i)    {
        for (int j = 0; j < m.dy; ++j)
            in >> m.p[i][j];
    }
    return in;
}

事实证明我的实现根本不正确。你能告诉我为什么这个实现是错误的吗?

我通过模仿现有的重载>>实现来实现上述部分,它已被证明在矩阵输出部分工作得很好,比如cout<<一个;其中 A 是矩阵

ostream &operator<<(ostream &out, const Matrix &m) 
{
    for (int i = 0; i < m.dx; ++i)    {
        for (int j = 0; j < m.dy; ++j)
            out << m.p[i][j] << "  ";
        out << endl;
    }
    return out;
}

I am trying to learn operator overloading by working on overloading >> for a matrix class to enable the key-board based input for a matrix by calling sth such as

Matrix M1;
cin >> M1;

The operator overloading part is given in the following

istream &operator>>(istream &in, Matrix &m) 
{
    for (int i = 0; i < m.dx; ++i)    {
        for (int j = 0; j < m.dy; ++j)
            in >> m.p[i][j];
    }
    return in;
}

It turns work that my implementation was not correct at all. Can you let me know why this implementation is wrong?

I implemented the above part by imitating an existing implementation of overloading >>, which has been proven to work fine in the matrix output part, like cout<< A; where A is a matrix

ostream &operator<<(ostream &out, const Matrix &m) 
{
    for (int i = 0; i < m.dx; ++i)    {
        for (int j = 0; j < m.dy; ++j)
            out << m.p[i][j] << "  ";
        out << endl;
    }
    return out;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

橘香 2024-10-31 19:29:09

我相信您的 operator >> 的问题在于您正在使用 Matrix 中已经存在的任何维度,而不是尝试从输入您找到的内容。

我认为最好的选择是让运算符 << 实现在矩阵前面添加维度信息(例如行数和列数),然后让运算符 > > 函数读取该信息。例如:

ostream &operator<<(ostream &out, const Matrix &m) 
{
   out << m.dx << ' ' << out.dy << '\n';
   for (int i = 0; i < m.dx; ++i)    {
      for (int j = 0; j < m.dy; ++j)
        out << m.p[i][j] << "  ";
      out << endl;
   }
   return out;
 }

有了这个,您可以将流提取运算符编写为:

istream &operator>>(istream &in, Matrix &m) 
{
   in >> m.dx >> m.dy;

   /* Some sort of logic to ensure that you've allocated an array large enough to
    * hold all the elements ...
    */

   for (int i = 0; i < m.dx; ++i)    {
       for (int j = 0; j < m.dy; ++j)
           in >> m.p[i][j];
   }
   return in;
}

这可能不是最美观的输入和输出运算符,但它们应该可以完成工作。

如果您想让这些运算符更加优雅,请考虑使用一些特殊字符来分隔行和列来输出矩阵的元素。例如,您可以尝试将矩阵输出

0 1 2
3 4 5
6 7 8

为:

[[0 1 2][3 4 5][6 7 8]]

通过此设置,有关矩阵的大小信息隐含在括号分组的工作方式中。话又说回来,这可能会使读取输入变得有点棘手,因为您事先不知道矩阵有多大。但总的来说,选择对自己来说最简单的事情。

仅供参考,您可能不想在编写流插入运算符时使用 endl 来分隔行。除了写入换行符之外,endl 还会刷新缓冲区。如果您的流已连接到网络连接,则每当您有新的矩阵行时,您可能不希望继续刷新缓冲区,因为这可能会导致大量数据突发(缓慢)发送,而不是对其进行分组一次全部集中在一起(快速)。

希望这有帮助!

I believe that the problem with your operator >> is that you're using whatever dimensions already happen to be in the Matrix rather than trying to recover the dimensions from the input that you find.

I think that your best bet would be to have the operator << implementation preface the matrix with dimension information (such as the number of rows and columns) and then have the operator >> function read in that information. For example:

ostream &operator<<(ostream &out, const Matrix &m) 
{
   out << m.dx << ' ' << out.dy << '\n';
   for (int i = 0; i < m.dx; ++i)    {
      for (int j = 0; j < m.dy; ++j)
        out << m.p[i][j] << "  ";
      out << endl;
   }
   return out;
 }

With this in hand, you can write your stream extraction operator as

istream &operator>>(istream &in, Matrix &m) 
{
   in >> m.dx >> m.dy;

   /* Some sort of logic to ensure that you've allocated an array large enough to
    * hold all the elements ...
    */

   for (int i = 0; i < m.dx; ++i)    {
       for (int j = 0; j < m.dy; ++j)
           in >> m.p[i][j];
   }
   return in;
}

This may not be the most aesthetically pleasing input and output operators, but they should get the job done.

If you want to make these operators a bit classier, consider outputting the elements of the matrix using some special character to delimit rows and columns. For example, you might try outputting the matrix

0 1 2
3 4 5
6 7 8

as

[[0 1 2][3 4 5][6 7 8]]

With this setup, the size information about the matrix is implicit in how the bracket grouping works. Then again, this may make it a bit trickier to read the input, since you wouldn't know in advance how large the matrix is. But go with what's easiest for yourself overall.

As an FYI, you probably don't want to use endl to delimit lines when writing a stream insertion operator. In addition to writing a newline, endl flushes the buffer. If your stream is hooked up to a network connection, you may not want to keep flushing the buffer whenever you have a new line of the matrix, since that could result in a lot of data getting sent in bursts (slow) rather than grouping it all together at once (fast).

Hope this helps!

蓝眼睛不忧郁 2024-10-31 19:29:09

我不认为你的代码有特别错误。
如果按下,我建议检查循环中的流条件。
供您参考,我测试时以下代码有效:

struct Matrix {
    static int const dx = 2, dy = 2;
    int p[ dx ][ dy ];
};

istream &operator>>(istream &in, Matrix &m) 
{
   for (int i = 0; i < m.dx; ++i)    {
       for (int j = 0; j < m.dy; ++j)
           if ( ! (in >> m.p[i][j]) ) return in;
 }
 return in;
}

int main() {
    Matrix M1;
    cin >> M1;
    cout << M1;
}

希望这有帮助

I don't think your code is wrong particularly.
If pressed I would suggest checking the stream condition in the loop.
For your information, the following code worked when I tested:

struct Matrix {
    static int const dx = 2, dy = 2;
    int p[ dx ][ dy ];
};

istream &operator>>(istream &in, Matrix &m) 
{
   for (int i = 0; i < m.dx; ++i)    {
       for (int j = 0; j < m.dy; ++j)
           if ( ! (in >> m.p[i][j]) ) return in;
 }
 return in;
}

int main() {
    Matrix M1;
    cin >> M1;
    cout << M1;
}

Hope this helps

江湖彼岸 2024-10-31 19:29:09
#include<iostream>

using namespace std;

class Array /*overload of subscript operator of 1D array*/
{
     private: int *p;
     public:
          int length;
          Array(int size = 0): length(size)
          {
                p=new int(length);
          }
          int& operator [](const int k)
          {
               return p[k];
          }
};
class Matrix
{
      private: Array *p;
      public: 
            int r,c;
            Matrix(int i=0, int j=0):r(i), c(j)
            {
                 p= new Array[r];
            }
            Array& operator [](const int& i)
            {
                 return p[i];
            }
            friend istream& operator >> (istream& in, Matrix& m);
          /*friend ostream& operator << (ostream& out, Matrix& m);*/
};
istream& operator >> (istream& in, Matrix& m)
{
     for(int i=0 ; i < m.r ; i++)
     {
         for(int j=0 ; j < m.c ; j++)
               in >> m[i][j];
     }
}
/*ostream& operator << (ostream& out, Matrix& m)
{
     for(int i=0 ; i < m.r ; i++)
     {
         for(int j=0 ; j < m.c ; j++)
               out << m[i][j] << " ";
         out << endl;
     }
}*/

/*Driver program*/
int main()
{
    Matrix M1(3,3); /*for checking purpose*/
    cin >> M1;
  /*cout << "\n" << M1;*/
}
#include<iostream>

using namespace std;

class Array /*overload of subscript operator of 1D array*/
{
     private: int *p;
     public:
          int length;
          Array(int size = 0): length(size)
          {
                p=new int(length);
          }
          int& operator [](const int k)
          {
               return p[k];
          }
};
class Matrix
{
      private: Array *p;
      public: 
            int r,c;
            Matrix(int i=0, int j=0):r(i), c(j)
            {
                 p= new Array[r];
            }
            Array& operator [](const int& i)
            {
                 return p[i];
            }
            friend istream& operator >> (istream& in, Matrix& m);
          /*friend ostream& operator << (ostream& out, Matrix& m);*/
};
istream& operator >> (istream& in, Matrix& m)
{
     for(int i=0 ; i < m.r ; i++)
     {
         for(int j=0 ; j < m.c ; j++)
               in >> m[i][j];
     }
}
/*ostream& operator << (ostream& out, Matrix& m)
{
     for(int i=0 ; i < m.r ; i++)
     {
         for(int j=0 ; j < m.c ; j++)
               out << m[i][j] << " ";
         out << endl;
     }
}*/

/*Driver program*/
int main()
{
    Matrix M1(3,3); /*for checking purpose*/
    cin >> M1;
  /*cout << "\n" << M1;*/
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文