【问题标题】:How to sort the row of a 2d matrix?如何对二维矩阵的行进行排序?
【发布时间】:2020-08-15 20:57:08
【问题描述】:

这是我的代码

cin >> n;

    vector<vector<int>> A(n, vector<int>(n));

    for (auto &row : A)
        for (auto &el : row)
            cin >> el;

    for (auto row : A)
        sort(row.begin(), row.end());

    for (auto row : A)
    {
        for (auto el : row)
            cout << el << " ";
        cout << "\n";
    }

例如,如果输入是:

3 ///3 by 3 matrix
1 2 3
2 1 3
3 2 1

输出应该是:

1 2 3 
1 2 3
1 2 3

我的代码给了我相同的输入,但我不知道如何修复它。

【问题讨论】:

  • 第一个循环 (for (auto &amp;row : A)) 和第二个循环 (for (auto row : A)) 之间有一个非常重要的区别

标签: c++ matrix multidimensional-array vector


【解决方案1】:

这样

for (auto& row : A)
    sort(row.begin(), row.end());

您正在尝试更改行,因此您需要对原始行的引用。您的代码只是对行的副本进行排序。

【讨论】:

    【解决方案2】:

    调用std::sort 时只使用引用而不是副本进行迭代。此外,在打印时最好使用引用,因为复制每一行会产生O(n) 的惩罚,其中n 是该行中的元素数。

    代码如下:

    #include <iostream>
    #include <algorithm>
    #include <vector>
    
    using namespace std; // this is not a good practice.
    
    int main() {
        int n; 
        cin >> n;
        vector<vector<int>> A(n, vector<int>(n));
        for (auto &row : A)
            for (auto &el : row)
                cin >> el;
        for (auto &row : A) //change this line
            sort(row.begin(), row.end());
        for (auto &row : A)
        {
            for (auto &el : row)
                cout << el << " ";
            cout << "\n";
        }
        return 0;
    }
    

    提问者要求我提供按列对矩阵进行排序的代码。代码如下:

    #include <iostream>
    #include <algorithm>
    #include <vector>
    
    void input_matrix(auto &x) {
        for (auto &i : x)
            for (auto &j : i)
                std::cin >> j;
    }
    
    void output_matrix(auto &x) {
        for (auto &i : x) {
            for (auto &j : i)
                std::cout << j << " ";
            std::cout << std::endl;
        }
    }
    
    void transpose_matrix(auto &x) {
        size_t n = x.size();
        for (size_t i = 0; i < n; i++)
            for (size_t j = i + 1; j < n; j++)
                std::swap(x[i][j], x[j][i]);
    }
    
    void sort_matrix_by_row(auto &x) {
        for (auto &i : x)
            std::sort(i.begin(), i.end());
    }
    
    void sort_matrix_by_col(auto &x) {
        transpose_matrix(x);
        sort_matrix_by_row(x);
        transpose_matrix(x);
    } 
    
    int main() {
        int n;
        std::cin >> n;
        std::vector<std::vector<int>> A(n, std::vector<int>(n));
        input_matrix(A);
        sort_matrix_by_col(A);
        output_matrix(A);
        return 0;
    }
    

    【讨论】:

    • 我也会对矩阵bool myfunction(vector&lt;int&gt; i, vector&lt;int&gt; j) { return (i[2] &lt; j[2]); } 的每一列进行排序,但这个函数只执行第二列。我需要对所有列进行排序。
    • @MogovanJonathan 您可以进行转置,然后按行排序,然后再次进行转置。这是最简单的方法。
    • 你能编辑你的答案并添加方法吗,因为我不知道怎么做。谢谢!
    • @MogovanJonathan 给我大约 5 分钟,我会添加它。
    • 你成功了吗?
    猜你喜欢
    • 2013-07-03
    • 1970-01-01
    • 1970-01-01
    • 2015-01-31
    • 2015-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多