c++中vector多维数组排序的方法是什么
导读:在C++中,可以使用std::sort函数对多维数组进行排序。下面是一个示例代码,对一个二维数组进行排序: #include <iostream> #include <vector> #include <alg...
在C++中,可以使用std::sort函数对多维数组进行排序。下面是一个示例代码,对一个二维数组进行排序:
#include <
iostream>
#include <
vector>
#include <
algorithm>
int main() {
std::vector<
std::vector<
int>
>
arr = {
{
3, 2, 1}
, {
6, 5, 4}
, {
9, 8, 7}
}
;
// 对二维数组按第一列进行排序
std::sort(arr.begin(), arr.end(), [](const std::vector<
int>
&
a, const std::vector<
int>
&
b) {
return a[0] <
b[0];
}
);
// 输出排序后的二维数组
for (const auto&
row : arr) {
for (int num : row) {
std::cout <
<
num <
<
" ";
}
std::cout <
<
std::endl;
}
return 0;
}
在上面的代码中,使用std::sort函数对二维数组按第一列进行升序排序。可以根据需要修改比较函数,对其他列进行排序。
声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!
若转载请注明出处: c++中vector多维数组排序的方法是什么
本文地址: https://pptw.com/jishu/677747.html