在C ++中将函数作为参数传递 [英] Passing functions as arguments in C++
问题描述
我对C ++有点生疏,经过一年的python转换.自然,我想将python的懒惰翻译成C ++.
I'm a bit rusty in C++ and I'm switching after a year of python. Naturally I would like to translate the laziness coming from python to C++.
我刚发现rot13,我对此感到非常兴奋.我找到了3种方法来做,我想做一点性能测试. 我还想看看在适当位置修改字符串或创建新字符串是否有区别.所以我最终有6个功能.
I just discovered rot13 and I'm all excited about it. I found 3 ways of doing it and I wanted to do a little performance test. I wanted to see also if there is a difference in modifying the string in place or creating a new one. So I ended up having 6 functions.
在第一种方法中,我使用std :: map来映射字符,因此,我建立了一个初始化映射的类,在第二种方法中,我使用了三元运算符,第三种方法是使用了位移.
In the first method, I use a std::map to map the characters, thus I've built a class that initializes the map, in the second I use a ternary operator, and the third I use a bit shift.
现在函数原型看起来像这样
Now the functions prototypes look like this
// map dependent
void Rot13::convert_inplace(string& mystr){
string Rot13::convert(const string& mystr){
// ternary operator
void bitrot_inplace(string& mystr){
string bitrot(const string& mystr){
// bit shift
void bitshiftrot_inplace(string& mystr){
string bitshiftrot(const string& mystr){
我想构造一个接受这些函数作为参数的函数,然后计算时间并打印结果
I wanted to construct a function that accept those functions as arguments to then calculate the time and print the results
所以我看了一下stackoverflow, 1 , 2 ,我想出了这个
So I had a look at stackoverflow, 1, 2, and I came up with this
typedef void (*vfc)(string str);
void printtime_inplace(string title, vfc func){
我尝试了这种构造,但是这意味着我受到vfc
返回类型(在我的情况下为void
或string
)的限制,并且受制于我需要传递类的指针的事实.
因此,我将必须做3个函数来容纳不同的函数,即用于类成员函数的函数,用于void返回类型的函数和用于字符串返回类型的函数.
I tried this construction yet this means I'm limited by the vfc
return type which in my case is either void
or string
, and by the fact I need to pass the pointer of the class.
Thus I will have to do 3 functions to accommodate the different functions, namely a function for the class member function, a function for the void return type and a function for the string return type.
所以我问自己,是否真的需要使用模板而不编写相同功能的3倍?我对模板真的不确定,但是我应该做3 typedefs
并构造printtime函数来接受模板吗?此外,有没有办法告诉模板您将只接受这些类型(即我定义的类型)?
So I asked myself, is this the case where I really need to use templates to not write 3 times the same function? I'm really not confident with templates but should I do 3 typedefs
and structure the printtime function to accept a template? Moreover is there a way to tell the template you will accept only these types (namely the one I defined)?
另一个问题,这可以说是一个好的设计吗?还是您会建议其他设计?另一种实现方式?
An other question, is this let's say a good design? or would you suggest an other design? An other implementation?
推荐答案
最简单的方法IMO是使用模板,而不是尝试编写具有具体类型的函数.
The easiest way, IMO, is to use a template instead of trying to write a function with a concrete type.
template<typename Function>
void printtime_inplace(string title, Function func)
{
//...
func(title);
//...
}
现在,您可以使用功能"中的任何内容.基本上,您可以将其传递给常规函数,函子,lambda,std::function
.编译器将为您标记出不同的实例,但是就您的代码而言,您正在调用同一函数.
This will now allow you to take anything that is a "function". You can pass it a regular function, a functor, a lambda, a std::function
, basically, any callable. The compiler will stamp out different instantiations for you but as far as your code is concerned you are calling the same function.
这篇关于在C ++中将函数作为参数传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!