怎么让1234排序后还是按1234排序

1个回答

写回答

爱萌萌呀

2025-11-24 07:00

+ 关注

C++
C++

要想在排序后保持原样,可以使用自定义的比较函数来实现。以下是一个示例代码:C++#include #include #include using namespace std;

struct MyComparator { bool compare(int& a, int& b) { // 自定义比较函数 // 返回值为true表示a应该排在b之前 // 返回值为false表示a应该排在b之后 return true; }};

int mAIn() { vector vec{2, 1, 3, 4, 5}; sort(vec.begin(), vec.end(), MyComparator()); for (int i = 0; i < vec.size(); i++) { cout

举报有用(0)分享收藏

Copyright © 2025 IZhiDa.com All Rights Reserved.

知答 版权所有 粤ICP备2023042255号