对二维数组进行排序 C++

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19237419/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 22:38:54  来源:igfitidea点击:

Sorting 2D Array C++

c++sorting

提问by 1110101001

Is it possible to sort a 2D Array using qsort or std::sort in C++ such that the elements are in increasing order when read from left to right in each row or from top to bottom in each column?

是否可以在 C++ 中使用 qsort 或 std::sort 对二维数组进行排序,以便在每行从左到右或在每列从上到下读取时元素按递增顺序排列?

For example,

例如,

13, 14, 15, 16
1, 4, 3, 2
7, 5, 7, 6
9, 10, 11, 12

Becomes:

变成:

{ 1, 2, 3, 4 }
{ 5, 6, 7, 8 }
{ 9, 10, 11, 12 }
{ 13, 14, 15, 16 } 

I know you can do it by creating two comparison functions and then first sorting each row then comparing the first elements of each row to establish the columns, but is there a way to do it in one function itself?

我知道你可以通过创建两个比较函数,然后首先对每一行进行排序,然后比较每行的第一个元素来建立列来做到这一点,但是有没有办法在一个函数本身中做到这一点?

回答by 4pie0

Yes. C++ STL library is built with separation of algorithmsand containers. What links them together is iterators. Raw pointer is iterator, therefore it is possible to initialize vector with raw pointers and then sort that vector as usual.

是的。C++ STL 库是建立在算法容器分离的基础上的。将它们连接在一起的是迭代器。原始指针是迭代器,因此可以使用原始指针初始化向量,然后像往常一样对该向量进行排序。

std::vector<int> v(arr2d, arr2d + N); // create a vector based on pointers
                                      // This assumes array is contiguous range 
                                      // in memory, N=number of elemnts in arr2d
// using default comparison (operator <):
std::sort (v.begin(), v.end());

// cout by 4 elements in a row

回答by Eslam

# include <iostream>
using namespace std ;


void swap (int &x , int &y)
{
    int temp = x ;
    x = y ;
    y = temp ;
}

void main ()
{
    int arr [3][3] = {{90,80,70},{60,50,40},{30,100,10}} ;
    int x ;

    for (int k = 0; k < 3; k++)
    {
        for (int m = 0; m < 3; m++)
        {
            x = m+1;
            for (int i = k; i < 3 ; i++)
            {
                for (int j = x; j < 3; j++)
                {
                    if (arr [k][m] > arr [i][j])
                        swap(arr [k][m] ,arr [i][j]);
                }
                x=0;
            } 
        }
    }

    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            cout << arr [i][j] << " ";
        }
    }


    system("pause");
}

C++ Sorting 2-D array ascendingly

C++ 对二维数组进行升序排序

回答by LxL

First Make a 2D vector .

首先制作一个二维向量。

Sort each vector in this 2D vector

对这个二维向量中的每个向量进行排序

Sort the whole vector

对整个向量进行排序

Code :

代码 :

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

template <class T>
void Sort2dArray(std::vector<std::vector<T>> & numbers)
{

    for(auto & i : numbers){//sort each vector<T> in numbers
        std::sort(i.begin(),i.end());
    }
    std::sort(numbers.begin(),numbers.end(),[](//sort numbers by defining custom compare 
              const std::vector<T>& a,const std::vector<T>&b){
        for(int i=0;i<a.size()&&i<b.size();i++)
        {
            if(a[i]>b[i])
                return false;
            else if(a[i]<b[i])
                return true;
        }
        return a.size()<b.size() ? true : false;
    });
}

int main()
{
    std::vector<std::vector<int>> numbers={ {13, 14, 15, 16},
                                            {1, 4, 3, 2},
                                            {8, 5, 7, 6},
                                            {9, 10, 12,11}};
    Sort2dArray(numbers);//sort array

    //write sorted array
    for(auto i:numbers)
    {
        for(auto j:i)
            std::cout<<j<<" ";
        std::cout<<"\n";
    }
}

回答by Macaire Alexander Bell

In theory you should be able to input the 16 numbers into an array. Use a for loop, maybe even a nested one, to sort the numbers. Then as for output you want the ascending numbers in four groups of four?

从理论上讲,您应该能够将 16 个数字输入到一个数组中。使用 for 循环,甚至可能是嵌套循环,对数字进行排序。那么对于输出你想要四组四组的升序数字?

cout<<Vector[0]<<Vector[1]<<Vector[2]<<Vector[3]<<endl;
cout<<Vector[4]<<Vector[5]<<Vector[6]<<Vector[7]<<endl;
cout<<Vector[8]<<Vector[9]<<Vector[10]<<Vector[11]<<endl;
cout<<Vector[12]<<Vector[13]<<Vector[14]<<Vector[15]<<endl;

very arbitrary but I'm not quite sure of the question.

非常随意,但我不太确定这个问题。

回答by varun

**Sorting 2D array in c++**

**在 C++ 中对二维数组进行排序**

#include <iostream>

using namespace std;

int main()
{
    int i,j,k,m,temp,n,limit;
    int** p;


    cout<<"Enter the limit:";
    cin>>limit;

    p=new int*[limit];

//inputing
    for(i=0;i<limit;i++)
    {
        p[i] = new int[limit];

        for(j=0;j<limit;j++)
        {
          cin>>p[i][j];
        }

    }

//sorting
    for(i=0;i<limit;i++)
    {
        for(j=0;j<limit;j++)
        {
            if (j==limit-1 && i<limit-1)
            {
                n =-1;
                m=i+1;
            }
            else
            {
                m=i;
                n=j;
            }

            for(k=n+1;k<limit;k++)
            {
                if(p[i][j] > p[m][k] )
                {
                    temp = p[i][j];
                    p[i][j] = p[m][k];
                    p[m][k] = temp;
                }

                if(k==limit-1 && m<limit-1) { m++;  k=-1;  }

            }
        }
    }

    //displaying
    for(i=0;i<limit;i++)
    {
        for(j=0;j<limit;j++)
        {
            cout<<p[i][j]<<endl;
        }

    }

    return 0;
}