请举个例子排序函数指针
时间: 2023-07-26 20:14:48 浏览: 61
假设有一个整数数组 `arr`,我们可以按照从大到小的顺序对其进行排序。下面是一个使用排序函数指针进行排序的例子:
```cpp
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
bool cmp(int a, int b) {
return a > b;
}
int main() {
vector<int> arr = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5};
sort(arr.begin(), arr.end(), cmp);
for (auto& x : arr) {
cout << x << " ";
}
cout << endl;
return 0;
}
```
在上面的例子中,我们定义了一个比较函数 `cmp`,它的功能是比较两个整数的大小,返回值为 `true` 表示第一个整数比第二个整数大。在 `main()` 函数中,我们通过 `sort` 函数对 `arr` 进行排序,其中第三个参数为指向 `cmp` 函数的指针,表示按照 `cmp` 函数的规则对 `arr` 进行排序。最终输出的结果是 `9 6 5 5 5 4 3 3 2 1 1`,满足从大到小的要求。
相关问题
请举个例子排序函数对象的成员函数指针:
假设有一个类 `Person`,其中有两个成员变量 `name` 和 `age`,我们可以按照年龄从小到大对一组 `Person` 对象进行排序。下面是一个使用排序函数对象的成员函数指针进行排序的例子:
```cpp
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class Person {
public:
string name;
int age;
Person(string n, int a) : name(n), age(a) {}
};
class Compare {
public:
bool operator()(const Person& p1, const Person& p2) const {
return p1.age < p2.age;
}
};
int main() {
vector<Person> people = {{"Alice", 25}, {"Bob", 30}, {"Charlie", 20}};
Compare c;
bool (Compare::*comp)(const Person&, const Person&) const = &Compare::operator();
sort(people.begin(), people.end(), c.*comp);
for (auto& p : people) {
cout << p.name << " " << p.age << endl;
}
return 0;
}
```
在上面的例子中,我们定义了一个比较函数对象 `Compare`,其中 `operator()` 是比较两个 `Person` 对象的函数。在 `main()` 函数中,我们首先创建了一个 `Compare` 对象 `c`,然后定义了一个成员函数指针 `comp`,指向 `Compare` 类的 `operator()` 函数。最后,我们通过 `c.*comp` 的方式将成员函数指针传递给 `sort` 函数,从而实现了按照年龄从小到大对 `people` 进行排序。
函数指针与指针函数举个例子
下面分别举例说明函数指针和指针函数的用法。
1. 函数指针的例子:
```c
#include <stdio.h>
int add(int a, int b) {
return a + b;
}
int minus(int a, int b) {
return a - b;
}
int main() {
int (*p)(int, int); // 声明一个函数指针
int a = 10, b = 5;
p = add; // 指向 add 函数
printf("%d + %d = %d\n", a, b, p(a, b));
p = minus; // 指向 minus 函数
printf("%d - %d = %d\n", a, b, p(a, b));
return 0;
}
```
在这个例子中,我们定义了两个函数 add 和 minus,它们都接受两个 int 类型的参数,返回一个 int 类型的值。然后我们声明了一个函数指针 p,它可以指向任何接受两个 int 类型的参数,返回一个 int 类型的函数。在 main 函数中,我们将 p 分别指向 add 和 minus 函数,并使用 p 调用对应的函数,输出结果。
2. 指针函数的例子:
```c
#include <stdio.h>
#include <stdlib.h>
int* generate_array(int size) {
int* arr = (int*)malloc(size * sizeof(int)); // 动态分配内存
for (int i = 0; i < size; i++) {
arr[i] = i + 1;
}
return arr; // 返回指针类型
}
int main() {
int size = 5;
int* arr = generate_array(size); // 调用指针函数生成数组
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]); // 输出数组
}
free(arr); // 释放内存
return 0;
}
```
在这个例子中,我们定义了一个指针函数 generate_array,它接受一个 int 类型的参数 size,返回一个 int 类型的指针。在 generate_array 函数中,我们通过 malloc 函数动态分配内存,生成一个长度为 size 的 int 数组,并将每个元素初始化为数组下标 + 1。最后,我们返回数组的指针。在 main 函数中,我们调用 generate_array 函数生成一个长度为 5 的数组,并输出数组的每个元素。最后,我们使用 free 函数释放数组的内存。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)