C++编程填空:矩阵排序

描述

创建矩阵类,要求能够输入整数类型的m*n矩阵,并按照元素个数,矩阵中元素之和,创建矩阵顺序对矩阵类分别排序

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

class Mat{
    int h,w;
public:
    Mat(int height,int width):h(height),w(width)
// 在此处补充你的代码
int main()
{
    vector<Mat> m;
    m.push_back(Mat(2,2));
    m.push_back(Mat(3,4));
    m.push_back(Mat(2,2));
    cin >> m[0] >> m[1] >> m[2];
    sort(m.begin(),m.end());
    cout<< m[0] << endl << m[1] << endl << m[2] <<endl;
    cout<<"*************"<<endl;
    sort(m.begin(),m.end(),comparator_1);
    cout<< m[0] << endl << m[1] << endl << m[2] <<endl;
    cout<<"*************"<<endl;
    sort(m.begin(),m.end(),comparator_2());
    cout<< m[0] << endl << m[1] << endl << m[2] <<endl;
    return 0;
}

输入

前两行是一个2*2矩阵

之后三行是一个3*4矩阵

最后两行是一个2*2矩阵

输出

先按照元素个数从小到大输出三个矩阵(大小相同时后创建的矩阵先输出)

再按照元素之和从小到大输出三个矩阵(大小相同时后创建的矩阵先输出)

再按照矩阵创建顺序从先到后输出三个矩阵

(矩阵排列方式与输入相同,每个元素后用一个空格进行分隔)

样例输入

2 3
3 4
 0 12 -3 -4
-2  2 -1  0
-1 -1 -1 -1
-1 3
-2 4

样例输出

-1 3
-2 4

2 3
3 4

0 12 -3 -4
-2 2 -1 0
-1 -1 -1 -1

*************
0 12 -3 -4
-2 2 -1 0
-1 -1 -1 -1

-1 3
-2 4

2 3
3 4

*************
2 3
3 4

0 12 -3 -4
-2 2 -1 0
-1 -1 -1 -1

-1 3
-2 4
// Q759043.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

class Mat{
    int h,w;
public:
    Mat(int height,int width):h(height),w(width)
// 在此处补充你的代码
    {
        p = new int[h * w];
        id = ++cnt;
    }
    int *p;
    int id;
    friend istream& operator>> (istream&, Mat&);
    friend ostream& operator<< (ostream&, Mat&);
    friend bool comparator_1(Mat a, Mat b);
    friend bool comparator_2(Mat a, Mat b);
    bool operator< (const Mat& other) const 
    {
        return h * w < other.h * other.w;
    }
    static int cnt;
};

int Mat::cnt = 0;

istream& operator >> (istream& input, Mat& m)
{
    for (int i = 0; i < m.h * m.w; i++)
        input >> m.p[i];
    return input;
}

ostream& operator<<(ostream& output, Mat& m)
{
    for (int i = 0; i < m.h; i++)
    {
        for (int j = 0; j < m.w; j++)
            output << m.p[i * m.w + j] << " ";
        output << endl;
    }
    return output;
}

bool comparator_1(Mat a, Mat b) {
    int sum1 = 0;
    for (int i = 0; i < a.h * a.w; i++)
        sum1 += a.p[i];
    int sum2 = 0;
    for (int i = 0; i < b.h * b.w; i++)
        sum2 += b.p[i];
    return sum1 < sum2;
}

bool comparator_2(Mat a, Mat b) {
    return a.id < b.id;
}

int main()
{
    vector<Mat> m;
    m.push_back(Mat(2,2));
    m.push_back(Mat(3,4));
    m.push_back(Mat(2,2));
    cin >> m[0] >> m[1] >> m[2];
    sort(m.begin(),m.end());
    cout<< m[0] << endl << m[1] << endl << m[2] <<endl;
    cout<<"*************"<<endl;
    sort(m.begin(),m.end(),comparator_1);
    cout<< m[0] << endl << m[1] << endl << m[2] <<endl;
    cout<<"*************"<<endl;
    sort(m.begin(),m.end(),comparator_2);
    cout<< m[0] << endl << m[1] << endl << m[2] <<endl;
    return 0;
}