c++ - 第 i 个元素返回 i 的函数数组
问题描述
昨天我的朋友挑战我用 C 语言编写一个函数,该函数将返回一个函数指针数组,其中第 i 个函数将返回 i。在 C++ 中很容易获得类似的效果,但我不确定如何在 C 中实现。
任何人都可以帮助我吗?
编辑。
我正在寻找的效果与此等效。
vector <function<int()>> get_functions(int n) {
vector <function<int()>> functions;
for (int i = 0; i < n; ++i) {
functions.emplace_back([i]() {
return i;
});
}
return functions;
}
int main() {
auto functions = get_functions(10);
for (auto f:functions) {
cout << f() << endl;
}
return 0;
}
编辑。
正如评论部分所问的那样,我提供了我对挑战的糟糕尝试。
typedef int (*fun_t)(void);
int fun() { return 0; }
int fun1() { return 1; }
fun_t *get_functions() {
fun_t *functions = malloc(sizeof(fun_t) * 2);
functions[0] = fun;
functions[1] = fun1;
return functions;
}
int main() {
fun_t* funs=get_functions();
for (int i = 0; i < 2; ++i) {
printf("%d\n",funs[i]());
}
free(funs);
}
解决方案
C++ 代码是作弊的。function<int()>
不是函数指针;事实上,它根本就不是一个指针,它是一个类。
因此,等效的 C 代码看起来像这样:
#include <stdio.h>
#include <stdlib.h>
// function<int ()>, simplified version just for this task
typedef struct {
int (*code)(int);
int ctx;
} function_int_t;
// function<int()>::operator()()
int call(function_int_t fun) {
return fun.code(fun.ctx);
}
// lambda body
int proto(int ctx) {
return ctx;
}
function_int_t *get_functions(size_t n) {
function_int_t *functions = calloc(n, sizeof *functions);
if (!functions) {
abort(); // hey, that's how C++ does it
}
for (size_t i = 0; i < n; i++) {
functions[i] = (function_int_t){ proto, i }; // capture i
}
return functions;
}
int main(void) {
size_t n = 10;
function_int_t *functions = get_functions(n);
for (size_t i = 0; i < n; i++) {
printf("%d\n", call(functions[i]));
}
free(functions);
return 0;
}
推荐阅读
- spring - 迁移到 Spring Boot 2.2.0 @JsonIgnore 不起作用
- android - FFmpegMediaMetadataRetriever.getFrameAtTime() 进展缓慢
- python - 在列表中查找相同的连续条目并查找有多少连续条目
- acumatica - 429 自定义错误模块无法识别此错误
- javascript - 不要在角度 8 中将我的配置注入核心模块
- python - 有没有办法从 sheetfu 获取实际的 A1 范围 - get_data_range()
- ios - 两个文本字段的 XCUIElement#typeText 失败
- python - 绘制数千个条形图时是否可以加快条形图的速度?
- python - 使用一维传感器输入创建 LSTM 层时遇到问题
- vb6 - 在表单中调用模块和类