我想知道如何使用 C++ 根据第一列对下面的三元组进行排序?我可以使用 std::map 吗?
0 0 1
1 2 0
2 0 3
0 1 4
想要的结果是
0 0 1
0 1 4
1 2 0
2 0 3
例如,您可以仅在 std::tuple 的向量上使用 std::sort - 默认比较是字典顺序的,因此第一列最重要。
假设您正在排序std::vector<std::vector<int>>
C++11:
std::sort(begin(vec), end(vec), [](const std::vector<int>& a,
const std::vector<int>& b){
return a[0] < b[0]; // sorting on the first column only
});
假设您想要词汇顺序:
std::sort(begin(vec), end(vec));