我想在一个名为 Map 的类中创建 MapIf
函数。 MapIf
将这样调用:
void addThree(int& n) {
n += 3;
}
class startsWith {
char val;
public:
startsWith(char v) : val(v) {};
bool operator()(const std::string& str) {
return str.length() && char(str[0]) == val;
}
};
int main(){
...
startsWith startWithB('B');
Map<std::string, int> msi;
MapIf(msi, startWithB, addThree);
return 0;
}
MapIf
的声明是什么?
void MapIf(const Map& map, class condition, void (*function)(ValueType));
这样可以吗?
最佳答案
以下应符合您的原型(prototype)。
template <typename Key, typename Value, typename Condition>
void MapIf(const Map<Key, Value>& map, Condition condition, void (*function)(Value&));
关于c++ - 获取函数指针和类指针的函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21090947/