如何对二维向量的列进行排序? [英] How to sort the column of a 2d vector?

查看:48
本文介绍了如何对二维向量的列进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每一列都需要排序,例如对于这个输入:

3//3乘3矩阵2 3 434 3 14 54 2

输出应该是

2 3 14 3 234 54 4

这是我的代码:

<预><代码>cin >>n;vector>A(n,向量int(n));for(自动和行:A)for (auto &el : row)cin >>埃尔;for (int i = 0; i

我的代码的问题在于它对一些列进行了排序,但不是全部.请帮我修复它

如果我输入上面第一个例子,我的输出是:

34 ​​3 14 54 22 3 4

只对最后一列进行排序

解决方案

问题是对于 for 循环的每次迭代,std::sort 可能更改已经排序的列.

例如,如果对列 i 进行排序,则列 i-1i-2 等可能会丢失用于对这些列进行排序.

无需更改太多原始代码,尽管这不是最有效的方法,但您可以创建一个辅助 std::vector<std::vector 和将循环内每次迭代的排序列结果保存在辅助向量中.

完成循环后,将辅助向量分配回原始向量.

#include #include <算法>#include std::vector>A = {{2, 3, 4}, {34, 3, 1}, {4, 54, 2}};int main(){如果 ( !A.empty() && !A[0].empty() ){自动 auxV = A;//辅助向量for (size_t i = 0; i < A[0].size(); i++){//对列 i 进行排序std::sort(A.begin(), A.end(), [&](vector<int>& l, vector<int>& j) {返回 (l[i] < j[i]);});//将i列排序的结果保存在辅助向量中for (size_t j = 0; j 

输出:

2 3 14 3 234 54 4

each column needs to be sorted, for example for this input:

3 //3 by 3 matrix

2 3 4
34 3 1
4 54 2

the output should be

2 3 1
4 3 2 
34 54 4

this is my code yet:


    cin >> n;

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

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

    for (int i = 0; i < n; i++)
        sort(A.begin(), A.end(), [&](vector<int>& l, vector<int>& j) {
            return (l[i] < j[i]); 
            });

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

the problem with my code is that it sorts some columns but not all. Please help me to fix it

if i put in the imput the first example above, my out put is :

34 3 1
4 54 2
2 3 4

only the last column get sorted

解决方案

The issue is that for each iteration of the for loop, the std::sort can potentially change the already sorted column.

For example, if you sort column i, then columns i-1, i-2, etc. can lose the changes that were made to sort those columns.

Without changing too much of your original code, and albeit not the most efficient way to do this, you could create an auxiliary std::vector<std::vector<int>> and save the sorted column results of each iteration inside the loop in the auxiliary vector.

When done with the loop, assign the auxiliary vector back to the original vector.

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

std::vector<std::vector<int>> A = {{2, 3, 4}, {34, 3, 1}, {4, 54, 2}};

int main()
{
    if ( !A.empty() && !A[0].empty() )
    {
        auto auxV = A; // The auxiliary vector<vector>

        for (size_t i = 0; i < A[0].size(); i++)
        {
            // Sort column i
            std::sort(A.begin(), A.end(), [&](vector<int>& l, vector<int>& j) {
                return (l[i] < j[i]); 
                });

            // Save the results of the sort of column i in the auxiliary vector  
            for (size_t j = 0; j < A.size(); ++j)
                auxV[j][i] = A[j][i];
        }
        A = auxV; // copy the results back to the original vector
   }

    // display results
    for (auto& row : A)
    {
        for (auto el : row)
            std::cout << el << " ";
        std::cout << "\n";
    }
}

Output:

2 3 1 
4 3 2 
34 54 4 

这篇关于如何对二维向量的列进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆