小编que*_*101的帖子

如何遍历函数向量并在 C++ 中调用它们中的每一个?

我正在尝试遍历存储在向量中的函数数组,并且我想通过迭代器指针对象调用它们中的每一个,但是类似这样:

itr->funcs[i](5); // call the ith index function && pass int 5 as param
Run Code Online (Sandbox Code Playgroud)

不是我猜的解决方案,那么解决方案是什么?

下面是我的代码,请检查代码中的最后一个 for 循环。

#include <iostream>
#include <string>
#include <vector>

// to be able to take other functions as arguments in a function
#include <functional> 

using namespace std;

// receive other functions inside a function
// syntax: return_type function_name(std::function<return_type(parameter_lists)>func_name, parameter_lists)
double doMath(std::function<double(double)> someOtherFunction, double num){
    return someOtherFunction(num);
}

double multBy2(double d){ 
    // b'coz x is pointing to multBy2 the word 'someOtherFunction' in the 
    // above function …
Run Code Online (Sandbox Code Playgroud)

c++ iterator stl vector c++11

3
推荐指数
1
解决办法
639
查看次数

标签 统计

c++ ×1

c++11 ×1

iterator ×1

stl ×1

vector ×1