填空题

对struct进行排序

发布于 2022-03-03 17:17:52

给定以下Point类,为其定义小于符号的操作符重载,使得我们可以用std::sort来对vector<Point>进行从小到大的排序(先按x从小到大排,然后按照y从小到大排),确保以下代码成功运行:

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

struct Point {
    int x
    int y
}

int main() {
        int x = 0
        int y = 0
        std::vector<Point> vec
        while (std::cin >> x >> y) {
                Point p
                p.x = x
                p.y = y
                vec.push_back(p)
        }

        std::sort(vec.begin(), vec.end())

        for (auto iter = vec.begin() iter != vec.end() ++iter) {
                std::cout << iter->x << " " << iter->y << std::endl
        }

        return 0
}

输入描述:
点的集合,一个点一行,每个点由x,y两个整数组成,x和y之间用空格分隔
输入样例: 4 6 3 8 4 2 9 6 8 3 9 8 输出描述: 输出格式和输入格式相同,但已经按从小到大排好序了输出样例 3 8 4 2 4 6 8 3 9 6 9 8
关注者
0
被浏览
9
知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看