在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函数对二维数组按第一列进行升序排序。可以根据需要修改比较函数,对其他列进行排序。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,请发送邮件至 55@qq.com 举报,一经查实,本站将立刻删除。转转请注明出处:https://www.szhjjp.com/n/1042787.html