首页 > 试题广场 >

(对二维数组排序)编写一个方法,使用下面的方法头对二维数组排

[问答题]
(对二维数组排序)编写一个方法,使用下面的方法头对二维数组排序:
 public static void sortfint m[][]) 
这个方法首先按行排序,然后按列排序。
例如:数组{{4, 2},{1,7},{4, 5},{1,2},{1,1},{4,1}}将被排序为{{1,1},{1, 2},{1, 7},{4, 1},{4, 2},{4, 5}}。
#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <set>
using namespace std;

class location
{
public:

    location(int x, int y)
    {
        this->x = x;
        this->y = y;
    }
    int x, y;
};


class compare_set
{
public:bool operator()(location p1, location p2)
{
    if (p1.x != p2.x)
        return p1.x < p2.x;
    else
        return p1.y < p2.y;
}
};

int main()
{
    multiset<location,compare_set> L;//multiset可以让键值是重复的
    int N;//首先输入坐标数目
    cin >> N;
    for (int i = 0; i < N; i++)
    {
        int x, y;
        cin >> x >> y;
        L.insert(location(x, y));
    }

    for (multiset<location>::iterator it = L.begin(); it != L.end(); it++)
    {
        cout << it->x << ' ' << it->y << endl;
    }


    return 0;
}
发表于 2022-09-25 16:26:27 回复(0)