【发布时间】:2026-01-12 03:55:01
【问题描述】:
问题陈述:
我想使用我的自定义排序标准对结构的std::vector 进行排序。
结构是:
struct Node
{
int x;
int y;
float value;
};
向量是:
std::vector<Node> vec;
我的自定义排序标准是矢量应首先按 y 排序,然后按 x 排序(就像在 Microsoft Excel 中一样)。
示例:
输入:
x y
5 6
2 4
1 1
1 0
8 10
4 7
7 1
5 4
6 1
1 4
3 10
7 2
输出:
x y
1 0
1 1
6 1
7 1
7 2
1 4
2 4
5 4
5 6
4 7
3 10
8 10
上述排序是否可以通过任何 C++ 标准库排序函数来实现?如果没有,我可以使用其他任何库吗?
【问题讨论】:
-
是的,您可以将
std::sort与自定义函子一起使用。请参阅此处的示例cplusplus.com/reference/algorithm/sort