Lambda 表达式与函数指针
函数指针:函数指针,或子程序指针,或过程指针,是指向函数的指针。简单来说,它是一个指向文本部分内部位置的指针。它存储函数的地址并用于将行为作为参数传递给另一个函数。
例如,如果有人想对像向量或列表这样的容器进行排序,并使用 STL sort(),但不希望按升序对其进行排序,这是默认参数,在这种情况下,传递一个行为到 sort 函数,它实际上是函数指针,并对其数据进行排序。
程序1:下面是实现上述概念的C++程序:
C++
// C++ program to implement the above
// concepts
#include
using namespace std;
// Descending order sorting function
int descending(int x, int y)
{
return x > y;
}
// Absolute value sorting function
int absolute(int x, int y)
{
return abs(x) > abs(y);
}
// Driver Code
int main()
{
// Stores integers in the vector
vector vect = { 2, 8, -5, -9,
0, 12, 5 };
cout << "Sorting with descending "
<< "order as parameter\n";
// Stores the address of descending
// order function in the funnptr
// function pointer
auto funptr = descending;
// Pass pointer in the actual function
sort(vect.begin(), vect.end(), funptr);
// Print the vector
for (auto i : vect)
cout << i << " ";
cout << "\n";
cout << "Sorting with absolute order"
<< " as parameter \n";
// Store the address of the absolute
// value function in the funnptr1
// function pointer
auto funptr1 = absolute;
// Pass pointer in actual function
sort(vect.begin(), vect.end(), funptr1);
// Print the vector
for (auto i : vect) {
cout << i << " ";
}
return 0;
}
C++
// C++ program to implement the above
// concepts
#include
using namespace std;
// Function having no parameter
void fun()
{
cout << "GeeksforGeeks\n";
}
// Function having two parameters
// and a return type
int add(int x, int y)
{
return x + y;
}
// Driver Code
int main()
{
// Function pointer declaration
// for a function having no *
// return type and no parameter
// note that this is the standard
// syntax used for declaring a
// function pointer
void (*funptr)() = fun;
// auto funptr = fun; can also be
// used
// Calling the function pointer
funptr();
cout << "\n";
// Function pointer declaration
// for a function having int as
// return type and two int parameter
int (*funptr1)(int, int) = add;
// Calling the function pointer
int x = funptr1(4, 6);
cout << x;
return 0;
}
C++
// C++ program to illustrate the
// above concept
#include
using namespace std;
// Driver Code
int main()
{
vector vect{ -1, -6, 4, 2, 0,
6, 3, 9, -5 };
cout << "Before sorting : \n";
for (auto i : vect) {
cout << i << " ";
}
cout << "\n";
cout << "Sorting in desceding "
<< "order \n";
sort(vect.begin(), vect.end(),
[](int a, int b) {
return a > b;
});
for (auto i : vect)
cout << i << " ";
cout << "\n";
cout << "Sorting with absolute "
<< "value as parameter\n ";
sort(vect.begin(), vect.end(),
[](int a, int b) {
return abs(a) > abs(b);
});
for (auto i : vect)
cout << i << " ";
cout << "\n";
return 0;
}
C++
// C++ program to illustrate the above
// concept
#include
using namespace std;
// Driver Code
int main()
{
int x = 0, a = 5, b = 4;
// In the below lambda expression,
// everything is captured by value,
// and only x by reference
auto fun = [=, &x](int x) {
// If one tries to manipulate
// the value of a read only
// variable, then it causes
// a compilation error.
x++;
// c is a new variable under
// the fun expression which
// sums up a and b
int c = a + b;
cout << "sum of the read only "
<< "variables is : " << c
<< "\n";
return x;
};
cout << "The value of x is " << fun(x);
return 0;
}
Sorting with descending order as parameter
12 8 5 2 0 -5 -9
Sorting with absolute order as parameter
12 -9 8 5 -5 2 0
为什么需要函数指针?函数指针用于以下目的:
- 它可以存储一个函数的地址。
- 像普通指针一样,函数指针可以传递给函数。
- 它可以用于 qsort(),一个用于排序的 C 编程库函数,以及 sort(),一个用于排序的 C++ STL 库函数。
- 它可用于实现虚拟功能。
方案2:下面是C ++调用无参数或使用函数指针具有两个参数的函数的函数的程序:
C++
// C++ program to implement the above
// concepts
#include
using namespace std;
// Function having no parameter
void fun()
{
cout << "GeeksforGeeks\n";
}
// Function having two parameters
// and a return type
int add(int x, int y)
{
return x + y;
}
// Driver Code
int main()
{
// Function pointer declaration
// for a function having no *
// return type and no parameter
// note that this is the standard
// syntax used for declaring a
// function pointer
void (*funptr)() = fun;
// auto funptr = fun; can also be
// used
// Calling the function pointer
funptr();
cout << "\n";
// Function pointer declaration
// for a function having int as
// return type and two int parameter
int (*funptr1)(int, int) = add;
// Calling the function pointer
int x = funptr1(4, 6);
cout << x;
return 0;
}
GeeksforGeeks
10
Lambda 表达式: Lambda 表达式是在 C++11 中引入的。引入这个背后的原因是为了消除使用函数指针时面临的复杂性或繁琐性。为了使用函数指针,需要创建一个单独的函数。之后,创建一个指向该函数的函数指针,然后将其作为参数传递给所需的函数。
由于使用函数指针执行的任务非常小,因此不值得写这么多行代码。因此,Lambda 表达式可以更轻松地完成相同的工作。
Lambda 表达式也称为匿名函数。它是包含在主函数的表达式,有助于将行为作为参数传递给函数。在默认情况下,它无权访问主函数存在的任何变量。要访问这些,需要对表达式的捕获列表进行一些修改。
句法:
[ capture list ]( parameters ) -> {
// Body
};
Lambda 表达式的构造:
- [ ]:捕获列表
- ( ):参数
- ->:箭头
- .:返回类型
- { }:函数体
程序 3:下面的程序说明了如何使用 lambda 表达式进行降序和绝对值排序。
C++
// C++ program to illustrate the
// above concept
#include
using namespace std;
// Driver Code
int main()
{
vector vect{ -1, -6, 4, 2, 0,
6, 3, 9, -5 };
cout << "Before sorting : \n";
for (auto i : vect) {
cout << i << " ";
}
cout << "\n";
cout << "Sorting in desceding "
<< "order \n";
sort(vect.begin(), vect.end(),
[](int a, int b) {
return a > b;
});
for (auto i : vect)
cout << i << " ";
cout << "\n";
cout << "Sorting with absolute "
<< "value as parameter\n ";
sort(vect.begin(), vect.end(),
[](int a, int b) {
return abs(a) > abs(b);
});
for (auto i : vect)
cout << i << " ";
cout << "\n";
return 0;
}
Before sorting :
-1 -6 4 2 0 6 3 9 -5
Sorting in desceding order
9 6 4 3 2 0 -1 -5 -6
Sorting with absolute value as parameter
9 6 -6 -5 4 3 2 -1 0
注意:在编写 lambda 表达式时,默认情况下,将无法访问写入表达式的变量。因此,要访问这些变量,需要在 lambda 表达式的捕获列表中进行以下更改。
Lambda 表达式中的捕获列表:
- [ ]:不捕捉任何东西。
- [ = ]:按值捕获所有内容。它只提供读取访问权限。
- [ & ]:通过引用捕获所有内容。它提供读取和写入访问权限。
- [ =, & x]:按值捕获所有内容,按引用捕获 x 变量。
- [ =x, & ]:按引用捕获所有内容,按值捕获 x。
程序 4:以下是说明捕获列表使用的程序:
C++
// C++ program to illustrate the above
// concept
#include
using namespace std;
// Driver Code
int main()
{
int x = 0, a = 5, b = 4;
// In the below lambda expression,
// everything is captured by value,
// and only x by reference
auto fun = [=, &x](int x) {
// If one tries to manipulate
// the value of a read only
// variable, then it causes
// a compilation error.
x++;
// c is a new variable under
// the fun expression which
// sums up a and b
int c = a + b;
cout << "sum of the read only "
<< "variables is : " << c
<< "\n";
return x;
};
cout << "The value of x is " << fun(x);
return 0;
}
sum of the read only variables is : 9
The value of x is 1