【问题标题】:Ordering 2d array in lexical order按词法顺序排列二维数组
【发布时间】:2020-12-05 21:59:36
【问题描述】:

我有一个二维字符数组,我正在尝试按字母顺序对它们进行排序。在每一行中都有一个字符组成的单词,我正在尝试对其进行排序。 我做了一些东西,但我不明白为什么这不起作用。如果您对我有解决方案,请解释您在做什么,以了解我为什么不成功。 谢谢!

char matrix[4][5] = {
                {'h','e','l','l','o'},
                {'r','e','a','d','y'},
                {'a','p','p','l','e'},
                {'p','o','i','n','t'},
                
    };
    char temp;
    bool flag = false;

    display(matrix);

    for (int i = 0; i < 4 - 1; i++)
    {
        for (int rows = 0; rows < 10-1; rows++)
    {
        flag = false;
        for (int cols = 0; cols < 5; cols++)
        {
            if (matrix[rows][cols] > matrix[rows + 1][cols])
            {
                flag = true;
                break;
            }
        }
        if (flag)
        {
            for (int index = 0; index < 5; index++)
                {
                        temp=matrix[rows][index];
                        matrix[rows][index]=matrix[rows+1][index];
                        matrix[rows+1][index]=temp;
                }
        }
    }
    }

【问题讨论】:

  • 是否允许使用另一个数组对矩阵进行“排序”?有一些方法可以在不实际对 2D 数组进行排序的情况下做你想做的事情。

标签: c++ arrays sorting


【解决方案1】:

我会发布这个答案,即使它可能不是您想要做的(但会对可能想要以这种方式做事的其他人有所帮助)。

不是对二维数组进行排序,而是不对它进行排序,而是对指向数组的索引数组进行排序。这比尝试操作数组本身要简单得多。

这是一个非常简单的例子:

#include <algorithm>
#include <cstring>
#include <iostream>

int main()
{
    char matrix[4][5] = {
                    {'h','e','l','l','o'},
                    {'r','e','a','d','y'},
                    {'a','p','p','l','e'},
                    {'p','o','i','n','t'},

    };

    // create the indices
    int index[] = { 0,1,2,3 };

    // sort the indices based on the data in the array 
    std::sort(index, index + 4, [&](int n1, int n2) 
          { return strncmp(matrix[n1], matrix[n2], 5) < 0; });

    // Output the results
    for (int i = 0; i < 4; ++i)
    {
        // Note how we access the original matrix using the index array
        std::cout.write(matrix[index[i]], 5);

        std::cout << " -- Using array at row " << index[i] << "\n";                    
   }
}

输出:

apple -- Using array at row 2
hello -- Using array at row 0
point -- Using array at row 3
ready -- Using array at row 1

最终结果表明,如果我们想以排序方式访问数组,索引只是指向将使用的行。原数组没有调整。

【讨论】:

    【解决方案2】:

    就是这样。

    #include <bits/stdc++.h>
    
    using namespace std;
    
    char matrix[4][5] = {
        {'h','e','l','l','o'},
        {'r','e','a','d','y'},
        {'a','p','p','l','e'},
        {'p','o','i','n','t'} };
    
    int n = 4, m = 5, k = 0;// number of rows and columns
    char temp;
    
    int main()
    {
        for ( int i = 0; i < n; i++ )
            for ( int j = i + 1; j < n; j++ )
            {
                k = 0;
    
                while ( matrix[i][k] == matrix[j][k] && k < m )
                    k++;
    
                if ( k < m && matrix[i][k] > matrix[j][k] ) // ASCII code comparison
                    for ( int k = 0; k < m; k++ )
                    {
                        temp = matrix[i][k];
                        matrix[i][k] = matrix[j][k];
                        matrix[j][k] = temp;
                    }
            }
    
        for ( int i = 0; i < n; i++ )
        {
            for ( int j = 0; j < m; j++ )
                cout << matrix[i][j];
            cout << "\n";
        }
    }
    

    【讨论】:

    • 谢谢。但我只能使用简单的数组来做到这一点
    • @AndreiRagman std::sort 适用于简单的数组。无需将值存储在向量中。
    • 你可以这样使用矩阵吗:char matrix[4][7] = { "hello", "ready", "apple", "point" };
    • 否,因为用户必须插入每个字符。我做这个数组是为了在我想执行的时候不要一直写同样的东西。你怎么能把这个数组用作一个字符而不是一个字符串?
    • @PaulMcKenzie 是的,但如果我不允许使用std::vector,我认为std::sort 也是不允许的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-17
    • 1970-01-01
    • 2021-09-25
    相关资源
    最近更新 更多