允许将仿函数或函数存储在类中 [英] Allowing a functor or a function to be stored in a class

查看:83
本文介绍了允许将仿函数或函数存储在类中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++ STL中,有很多地方可以调用函数,即
将函数作为参数。这个仿函数可以是函数对象,也可以是函数指针。例如,如果你有一个向量< char>并且打电话给for_each

,以下两种方法都是有效的:


#include< iostream>

#包含< vector>

使用命名空间std;


struct MyFunctionObject

{

void operator ()(char a){cout<< a;};

};

void myFunction(char a){cout<< a;}


int main()

{

vector< char> v;

//填充它。 。 。

for_each(v.begin(),v.end(),(MyFunctionObject()));

for_each(v.begin(),v.end( ),& myFunction);

返回0;

}


我的问题与类似问题有关:我该怎么做导致同样发生在

类模板中?我希望我的模板看起来像这样:


模板< typename CompT>

类WhyWontThisWork

{

CompT m_comparator;

public:

WhyWontThisWork(CompT cmpIn):m_comparator(cmpIn){}

void doThis(char a,char b){if(m_comparator(a,b))cout<< " WIN英寸; }

};


我可以在没有编写过多代码的情况下在C ++中实际执行此操作吗?如果

那么,怎么样?


- JFA1

In the C++ STL, there are various places where you can call a function that
takes a functor as a parameter. This functor may be either a function object or
a function pointer. For instance, if you have a vector<char> and call for_each
on it, both of the methods below would be valid:

#include <iostream>
#include <vector>
using namespace std;

struct MyFunctionObject
{
void operator ()(char a) {cout << a;};
};
void myFunction(char a) {cout << a;}

int main()
{
vector<char> v;
//fill it . . .
for_each(v.begin(), v.end(), (MyFunctionObject()));
for_each(v.begin(), v.end(), &myFunction);
return 0;
}

My question pertains to a similar issue: how do I cause the same to occur in a
class template? I would like my template to look like this:

template <typename CompT>
class WhyWontThisWork
{
CompT m_comparator;
public:
WhyWontThisWork(CompT cmpIn) : m_comparator(cmpIn) {}
void doThis(char a, char b) { if (m_comparator(a, b)) cout << "WIN"; }
};

Can I actually do this in C++ without writing an inordinate amount of code? If
so, how?

- JFA1

推荐答案

James Aguilar写道:
James Aguilar wrote:

我的问题与类似的问题有关:如何在
类模板中出现相同的问题?我希望我的模板看起来像这样:

模板< typename CompT>
类WhyWontThisWork
{
CompT m_comparator;
public:
WhyWontThisWork(CompT cmpIn):m_comparator(cmpIn){}
void doThis(char a,char b){if(m_comparator(a,b))cout<< " WIN英寸; }
};

我是否可以在不编写过多代码的情况下在C ++中实际执行此操作?如果
那么,怎么样?

My question pertains to a similar issue: how do I cause the same to occur in a
class template? I would like my template to look like this:

template <typename CompT>
class WhyWontThisWork
{
CompT m_comparator;
public:
WhyWontThisWork(CompT cmpIn) : m_comparator(cmpIn) {}
void doThis(char a, char b) { if (m_comparator(a, b)) cout << "WIN"; }
};

Can I actually do this in C++ without writing an inordinate amount of code? If
so, how?




这很好用。你遇到了什么问题?


struct Comp

{

bool operator()(char a,char b)const {返回a == b; }

};


int main()

{

Comp comp;

WhyWontThisWork< Comp> cmp(comp);

cmp.doThis(''a'',''b'');

cmp.doThis(''a'','' a'');

返回0;

}


-


Pete Becker

Dinkumware,Ltd。( http://www.dinkumware。 com))



This works just fine. What problem did you encounter?

struct Comp
{
bool operator()(char a, char b) const { return a == b; }
};

int main()
{
Comp comp;
WhyWontThisWork<Comp> cmp(comp);
cmp.doThis(''a'', ''b'');
cmp.doThis(''a'', ''a'');
return 0;
}

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)


James Aguilar写道:
James Aguilar wrote:
在C ++ STL中,有很多地方你可以调用一个函数
将仿函数作为参数。该仿函数可以是函数对象,也可以是函数指针。例如,如果你有一个向量< char>并调用for_each
,下面的两种方法都是有效的:

#include< iostream>
#include< vector>
使用命名空间std ;

struct MyFunctionObject
{
void operator()(char a){cout<< a;};
};
void myFunction(char a){cout<< a;}

int main()
{
vector< char> v;
//填写它。 。 。
for_each(v.begin(),v.end(),(MyFunctionObject()));
for_each(v.begin(),v.end(),& myFunction);
返回0;
}
我的问题与类似的问题有关:如何在
类模板中出现相同的问题?我希望我的模板看起来像这样:

模板< typename CompT>
类WhyWontThisWork
{
CompT m_comparator;
public:
WhyWontThisWork(CompT cmpIn):m_comparator(cmpIn){}


我不明白''为什么不要这个工作''它的一点点。

无效doThis(char a,char b){if(m_comparator(a,b))cout<< " WIN英寸; }
};

我是否可以在不编写过多代码的情况下在C ++中实际执行此操作?


什么是过度的?


我甚至没有看到你试图使用你的模板。我们如何在没有你尝试的情况下继续进行讨论?

如果
那么,怎么样?
In the C++ STL, there are various places where you can call a function that
takes a functor as a parameter. This functor may be either a function object or
a function pointer. For instance, if you have a vector<char> and call for_each
on it, both of the methods below would be valid:

#include <iostream>
#include <vector>
using namespace std;

struct MyFunctionObject
{
void operator ()(char a) {cout << a;};
};
void myFunction(char a) {cout << a;}

int main()
{
vector<char> v;
//fill it . . .
for_each(v.begin(), v.end(), (MyFunctionObject()));
for_each(v.begin(), v.end(), &myFunction);
return 0;
}

My question pertains to a similar issue: how do I cause the same to occur in a
class template? I would like my template to look like this:

template <typename CompT>
class WhyWontThisWork
{
CompT m_comparator;
public:
WhyWontThisWork(CompT cmpIn) : m_comparator(cmpIn) {}
I don''t understand ''WhyWontThisWork'' bit of it.
void doThis(char a, char b) { if (m_comparator(a, b)) cout << "WIN"; }
};

Can I actually do this in C++ without writing an inordinate amount of code?
What''s inordinate?

I don''t see you even attempting to use your template. How can we continue
the discussion without you even trying?
If
so, how?




你读的哪本书没有解释模板?什么部分的

常见问题解答关于一些不起作用的代码的问题,不是吗?b
懂吗?我使用了过长的句子吗?


V



What book are you reading that doesn''t explain templates? What section of
the FAQ regarding questions about some code that doesn''t work don''t you
understand? Am I using overly long sentences?

V




" Victor Bazarov" <五******** @ comAcast.net>在消息中写道

新闻:IP ****************** @ newsread1.mlpsca01.us.to .verio.net ...

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:IP******************@newsread1.mlpsca01.us.to .verio.net...

我甚至没有看到你试图使用你的模板。我们怎样才能继续讨论,你甚至不用尝试?

I don''t see you even attempting to use your template. How can we continue
the discussion without you even trying?




好​​的。


// ===代码===

#include< iostream>


使用命名空间std;


template< typename CompT>

class Test

{

CompT m_comparator;

public:

测试(CompT cmpIn):m_comparator(cmpIn){}

void doThis(char a,char b){if(m_comparator(a,b))cout<< " WIN英寸; }

};


类TestFunctionObject

{

public:

bool operator()(char a,char b){return a< b;}

};


bool testFunction(char a,char b)

{

返回< b;

}


int main()

{

测试a(& testFunction );

测试b((TestFunctionObject()));


a.doThis('''',''b'');

b.doThis('''',''b'');


返回0;

}

// ===代码===


由于显而易见的原因,此代码无法编译。我没有为主要的a或b提供模板

类型。但这确实是我要问的问题:它是否可以让我有一个类,可以在构造函数中使用函数指针或函数

对象并对待它他们以同样的方式(即将它们存储起来以后再用

在另一个函数中使用)?如果是这样,我需要用什么类型替换

CompT?


- JFA1



OK.

//=== CODE ===
#include <iostream>

using namespace std;

template <typename CompT>
class Test
{
CompT m_comparator;
public:
Test(CompT cmpIn) : m_comparator(cmpIn) {}
void doThis(char a, char b) { if (m_comparator(a, b)) cout << "WIN"; }
};

class TestFunctionObject
{
public:
bool operator ()(char a, char b) {return a < b;}
};

bool testFunction(char a, char b)
{
return a < b;
}

int main()
{
Test a(&testFunction);
Test b((TestFunctionObject()));

a.doThis(''a'', ''b'');
b.doThis(''a'', ''b'');

return 0;
}
//=== CODE ===

For obvious reasons, this code does not compile. I didn''t provide template
types for a or b in main. But that is really the question I am asking: is it
possible for me to have a class that can take a function pointer or a function
object in the constructor and treat them the same way (i.e. store them for later
use in another function)? If so, what is the type I need to use to replace
CompT?

- JFA1


这篇关于允许将仿函数或函数存储在类中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆