如何按最后一列对2D矩阵进行排序

时间:2018-11-30 14:14:46

标签: c++ sorting matrix

如何使用可比函数而不是位置数组按最后一列对2D矩阵排序?

4 20 15 23 18 9 89 1 8 23 22 14 18 86 17 15 13 18 12 15 90 3 18 8 20 12 5 66

这是我的2D矩阵示例,其中最后一列表示 i = 1-> n 行的元素之和。我必须对行进行升序排序,比较最后一列的元素。

编辑!

第一个代码是这个:

int main()
{
    int x[101][101],y[101],z[101],n,m;
    int i,I,j,l,Mi=1000001,b=0;
    int s=0;

    cin>>n>>m;

    for(i=1;i<=n;i++)
        for(I=1;I<=m;I++)
            cin>>x[i][I];
    for(i=1;i<=n;i++)
    {
        s=0;
        for(I=1;I<=m;I++)
            s=s+x[i][I];
        y[i]=s;
    }
    for(l=1;l<=n;l++)
    {
        Mi=1000001;
        for(j=1;j<=n;j++)
            if(y[j]<Mi)
            {
                Mi=y[j];
                b=j;
            }
        z[l]=b;
        y[b]=1000002;
    }

    for(i=1;i<=n;i++)
    {
        for(j=1;j<=m;j++)
            cout<<x[z[i]][j]<<" ";
        cout<<endl;
    }
    return 0;
}

但是正如我所说..我使用的位置数组并不是最好的选择,因为它们占用大量空间,我不希望这样。 该数组是一个整数数组。我被困住了,因为我不知道另一种方法,这就是为什么我想寻求您的帮助。

2 个答案:

答案 0 :(得分:1)

看这个简单的例子。我建议使用std :: vector来存储矩阵。 要进行排序,请使用带有已定义比较lambda( cmp )的std :: sort。

#include <iostream>
using namespace std;

const int C = 5, R = 5;
using row = vector<int>;
using matr = vector<row>;

struct matrix
{
    matr m_matrix;
    matrix(int row, int col)
    {
        m_matrix.resize(row);
        for(auto &a : m_matrix)
            a.resize(col);
    }
    void set(int r, int c, int val)
    {
        m_matrix[r][c] = val;
    }
    void sort()
    {
        auto cmp = [](const row &r1, const row &r2)
        {
            return r1[r1.size() - 1] < r2[r2.size() - 1];
        };
        std::sort(m_matrix.begin(), m_matrix.end(), cmp);
    }
    void print()
    {
        for(auto &a : m_matrix)
        {
            for(auto v : a)
                cout << v << "  ";
            cout << endl;
        }
    }
};


void fill_matrix(matrix &m)
{
    int cntr = 15;
    for(int i = 0;i < C;i++)
    {
        int sum = 0;
        for(int j = 0;j < R - 1;j++)
        {
            int value = 10 + (cntr++) % 20;
            m.set(i, j, value);
            sum += value;
        }
        m.set(i, R - 1, sum);
    }
}


int main(int argc, char* argv[])
{
    matrix m(5, 5);
    fill_matrix(m);    
    m.print();
    cout << endl;
    m.sort();
    m.print();
}

结果:

enter image description here

答案 1 :(得分:1)

首先-使用std::array<T, size>而不是C样式的方括号数组(T[])。用法:

std::array<std::array<int, 7>, 4> matrix{
        {
                {{4, 20, 15, 23, 18, 9, 89}},
                {{1, 8, 23, 22, 14, 18, 86}},
                {{17, 15, 13, 18, 12, 15, 90}},
                {{3, 18, 8, 20, 12, 5, 66}}
        }
};

尽管乍一看令人困惑的括号数量令人困惑,但是当您意识到工具std::array的强大功能时,您会很快忘记它。

第二个-使用标准算法。您需要:

  • 获取给定行的最后一个元素
  • 获取另一行的最后一个元素
  • 定义一个比较函数,该函数将比较上述元素以有效地比较整个行
  • 将该功能与标准排序算法结合使用:std::sort

建议的解决方案:

std::sort(matrix.begin(), matrix.end(), [](const auto& first_row,
                                           const auto& second_row) {
    return first_row.back() < second_row.back();
});

我们使用 lambda表达式定义一个比较器,该比较器将比较两行。这就是std::sort的全部需求-一种比较范围内的两个元素的方法-在这种情况下,我们定义了如何比较矩阵的两个(常见的C++一种方法是使用小于运算符提供严格的弱排序-这就是使用<比较最后一个元素或每一行的原因。如果要反转顺序,只需使用><=>=,因为它们不提供严格的排序)。

相关问题