C++对vector对象数组进行排序的同时自定义排序规则
时间: 2023-12-23 15:04:08 浏览: 100
可以使用STL库中的sort函数对vector对象数组进行排序并自定义排序规则。示例代码如下:
```cpp
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
bool myCompare(int a, int b) {
return a > b; // 降序排序
}
int main()
{
vector<int> vec {5, 1, 3, 2, 4};
// 使用sort函数进行自定义排序
sort(vec.begin(), vec.end(), myCompare);
// 输出排序后的结果
for (auto num : vec) {
cout << num << " ";
}
return 0;
}
```
输出结果为:5 4 3 2 1。
在代码中,我们定义了一个自定义函数myCompare,该函数的返回值类型为bool型,接受两个int型参数a和b,用于自定义排序规则。在主函数中,我们首先定义了一个vector对象数组vec,其中包含了5个整型元素。然后我们使用sort函数对vec进行排序,并传入自定义函数myCompare作为第三个参数,即用于自定义排序规则。最后使用for循环输出排序后的结果。
本例中自定义函数myCompare为降序排序,如果需要进行升序排序,则返回值应为a < b。
相关问题
C++vector数组指定数组范围排序
可以使用标准库函数 `qsort` 进行排序,需要自定义一个比较函数来指定排序规则。
比如,对于一个 `int` 类型的数组 `arr`,要对其中从下标 `left` 到下标 `right` 之间的元素进行升序排序,可以这样实现:
```c
int cmp(const void *a, const void *b) {
return *(int *)a - *(int *)b;
}
void sort_range(int *arr, int left, int right) {
qsort(arr + left, right - left + 1, sizeof(int), cmp);
}
```
其中,`cmp` 函数返回值为负数表示 `a` 应该排在 `b` 前面,返回值为正数表示 `a` 应该排在 `b` 后面,返回值为 0 表示 `a` 和 `b` 相等。
`sort_range` 函数中,`qsort` 函数的第一个参数是指向要排序的数组第一个元素的指针,需要加上 `left` 才能指向指定的范围;第二个参数是要排序的元素个数,即 `right - left + 1`;第三个参数是每个元素的大小,即 `sizeof(int)`;第四个参数是比较函数的指针,即 `cmp`。
调用方式如下:
```c
int arr[] = {9, 2, 5, 7, 3};
int left = 1, right = 3;
sort_range(arr, left, right);
```
这样就可以将 `arr` 数组中下标从 `left` 到 `right` 的元素排序,结果为 `{9, 2, 5, 7, 3}` 变为 `{9, 2, 3, 5, 7}`。
C++对对象数组进行排序
可以使用STL库中的sort函数对对象数组进行排序。以学生类为例,示例代码如下:
```cpp
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class Student {
public:
string name;
int score;
Student(string _name, int _score)
: name(_name), score(_score) {}
bool operator < (const Student& other) const {
return this->score < other.score;
}
};
int main()
{
vector<Student> vec {{"Alice", 85}, {"Bob", 90}, {"Cindy", 80}, {"David", 95}};
// 使用sort函数进行排序
sort(vec.begin(), vec.end());
// 输出排序后的结果
for (auto stu : vec) {
cout << stu.name << " " << stu.score << " ";
}
return 0;
}
```
输出结果为:Cindy 80 Alice 85 Bob 90 David 95。
在代码中,我们定义了一个学生类Student,包含了学生姓名和分数两个成员变量,并定义了小于运算符重载函数,用于自定义排序规则。在主函数中,我们首先定义了一个学生类对象数组vec,其中包含了4个学生对象。然后我们使用sort函数对vec进行排序,sort函数会自动调用类的小于运算符重载函数进行排序。最后使用for循环输出排序后的结果。
本例中自定义排序规则为按照学生成绩从小到大排序,如果需要按照其他规则进行排序,只需要修改类中的小于运算符重载函数即可。
阅读全文
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)