这是一个使用qSort函数进行排序的例子:
#include <iostream>
#include <vector>
#include <algorithm>
// 自定义比较函数,用于指定排序规则
bool myLessThan(int a, int b) {
return a < b; // 升序排序
}
int main() {
std::vector<int> nums = {5, 3, 1, 4, 2};
// 调用 qSort 进行排序
std::qSort(nums.begin(), nums.end(), myLessThan);
// 打印排序后的结果
for (const auto& num : nums) {
std::cout << num << " ";
}
std::cout << std::endl;
return 0;
}
在上面的例子中,我们定义了一个自定义的比较函数 myLessThan 来指定升序排序规则。然后我们创建了一个包含一些整数的向量 nums,并将其作为参数传递给 qSort 函数。最后,我们打印出排序后的结果。
内容由零声教学AI助手提供,问题来源于学员提问




