类型函数 [英] Type Functors

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

问题描述

我有这堂课:


班级考试{

public:

int getpp()

{

返回pp;

}


void setpp(int i){

pp = i;

}

私人:

int pp;

};

现在我想把它插入一套:


std :: set< test,std :: less< test my_set;

test t1;

my_set.insert(t1);


这不起作用(缺少''<''运算符)所以我添加以下内容测试:

班级考试{

公开:

int getpp()

{

返回pp;

}


void setpp(int i){

pp = i;

}


bool运算符<(test const& t)const {


return(* this).getpp()< ; t.getpp();

}


私人:

int pp;

};


哪个也行不通。


std :: less实现类似:


bool operator()(T const& x,T const& y)const {

return x< y;

}

所以当我提供

std :: less< testto my_set。这似乎是双重工作。有什么问题

的主体:


test :: operator<(test const& t)const

解决方案

desktop写道:


我有这个课程:


class test {

公开:

int getpp()

{

返回pp;

}


void setpp(int i){

pp = i;

}


private:

int pp;

};


现在我想将它插入一个集合中:


std :: set< test,std :: less< test my_set;



std :: less< T是该模板参数的默认值。你不需要b $ b需要明确指定它(尽管它没有受到伤害)。唯一的时间

你将使用第二个模板参数是你的类型没有

实现运算符<或者你想订购除了

运营商<。因为您正在实施运营商<在下面你可以改变

这只是:


std :: set< testmy_set;


test t1;

my_set.insert(t1);


这不起作用(缺少''<''运算符)所以我添加以下内容进行测试:


class test {

public:

int getpp()



此函数不会更改类的状态。它应该是

声明''const'':

int getpp()const


{

返回pp;

}


void setpp(int i){

pp = i; < br $> b $ b}

bool运算符<(test const& t)const {


return(* this)。 getpp()< t.getpp();



如果没有上述更改,则无法执行此操作。运营商LT;一直是

声明为const,所以你不能调用非const成员函数。


}


私人:

int pp;

};


这也不起作用。

std :: less实现类似的东西:


bool operator()(T const& x,T const& y)const {

返回x< y;


}

所以我不明白为什么我需要在''测试'中定义''<'''当我供应

std :: less< testto my_set。这似乎是双重工作。有什么问题

的主体:


test :: operator<(test const& t)const



-

Alan Johnson


Alan Johnson写道:


desktop写道:


>我有这堂课:

班级考试{
公开:
int getpp()
{
返回pp;
}
void setpp(int i){
pp = i;
}
<私人:
int pp;
};

现在我想把它插入一套:

std :: set< test,std :: less< test my_set;



std :: less< T是该模板参数的默认值。你不需要b $ b需要明确指定它(尽管它没有受到伤害)。唯一的时间

你将使用第二个模板参数是你的类型没有

实现运算符<或者你想订购除了

运营商<。



插入时似乎出现问题:


test t1;

my_set.insert(t1);


只要我不插入我可以制作一套有或没有指定

std :: less< test并没有在测试中实现''<''。

在进行插入时我必须在测试中实现''<''运算符并且它

没有区别,如果我指定std :: less< testor not。


但我不知道该放在''<''的主体中。目前我有:


bool运算符<(test const& t)const {

return(* this).getpp()< t.getpp();

}


但这只是为了填补身体。


< blockquote> On 2007-06-08 18:30,desktop写道:


我有这个课程:


班级考试{

public:

int getpp()

{

返回pp;

}


void setpp(int i){

pp = i;

}


private:

int pp;

};


现在我想将它插入到一个集合中:


std :: set< test,std :: less< test my_set;

test t1;

my_set.insert( t1);


这不起作用(缺少''<''运算符)所以我添加以下内容进行测试:


课堂测试{

public:

int getpp()

{

返回pp;

}


void setpp(int i){

pp = i;

}


bool运算符<(测试c onst&安培; t)const {


return(* this).getpp()< t.getpp();

}


私人:

int pp;

};


哪个也行不通。


std :: less实现类似:


bool operator()(T const& x,T const& y)const {

return x< y;


}

所以我不明白为什么我需要在''测试'中定义''<'''当我供应

std :: less< testto my_set。这似乎是双重工作。



通过使用std :: less作为集合中的比较器,你说你想要

使用你的<操作员,(因为std :: less会调用

你的<操作员)。


-

Erik Wikstr?m


I have this class:

class test {
public:
int getpp()
{
return pp;
}

void setpp(int i) {
pp = i;
}
private:
int pp;
};
Now I would like to insert it into a set:

std::set<test, std::less<test my_set;
test t1;
my_set.insert(t1);

This does not work (missing a ''<'' operator) so I add the following to test:
class test {
public:
int getpp()
{
return pp;
}

void setpp(int i) {
pp = i;
}

bool operator <(test const& t) const {

return (*this).getpp() < t.getpp();
}

private:
int pp;
};

Which does not work either.

std::less implements something like:

bool operator() (T const& x, T const& y) const {
return x < y;
}
So I don''t quite see why I need to define ''<'' in ''test'' when I supply
std::less<testto my_set. It seems like double work. What is wrong in
the body of:

test::operator<(test const& t) const

解决方案

desktop wrote:

I have this class:

class test {
public:
int getpp()
{
return pp;
}

void setpp(int i) {
pp = i;
}
private:
int pp;
};
Now I would like to insert it into a set:

std::set<test, std::less<test my_set;

std::less<Tis the default for that template parameter. You do not
need to explicitly specify it (though it doesn''t hurt). The only time
you''ll use the second template parameter is if your type does not
implement operator< or you want an ordering other than that imposed by
operator<. Because you are implementing operator< below you can change
this to just:

std::set<testmy_set;

test t1;
my_set.insert(t1);

This does not work (missing a ''<'' operator) so I add the following to test:
class test {
public:
int getpp()

This function does not change the state of the class. It should be
declared ''const'':
int getpp() const

{
return pp;
}

void setpp(int i) {
pp = i;
}

bool operator <(test const& t) const {

return (*this).getpp() < t.getpp();

Without the above change, you cannot do this. operator< has been
declared const, so you cannot call non-const member functions.

}

private:
int pp;
};

Which does not work either.

std::less implements something like:

bool operator() (T const& x, T const& y) const {
return x < y;
}
So I don''t quite see why I need to define ''<'' in ''test'' when I supply
std::less<testto my_set. It seems like double work. What is wrong in
the body of:

test::operator<(test const& t) const


--
Alan Johnson


Alan Johnson wrote:

desktop wrote:

>I have this class:

class test {
public:
int getpp()
{
return pp;
}
void setpp(int i) {
pp = i;
}

private:
int pp;
};
Now I would like to insert it into a set:

std::set<test, std::less<test my_set;


std::less<Tis the default for that template parameter. You do not
need to explicitly specify it (though it doesn''t hurt). The only time
you''ll use the second template parameter is if your type does not
implement operator< or you want an ordering other than that imposed by
operator<.

The problem seems to occur when I do an insert:

test t1;
my_set.insert(t1);

as long as I don''t insert I can make a set with or without specifying
std::less<testand without implementing the ''<'' in test.

When doing an insert I must implement the ''<'' operator in test and it
makes no difference if I specify std::less<testor not.

But I am not sure what to put in the body of ''<''. Currently I have:

bool operator <(test const& t) const {
return (*this).getpp() < t.getpp();
}

but that was just to fill in the body.


On 2007-06-08 18:30, desktop wrote:

I have this class:

class test {
public:
int getpp()
{
return pp;
}

void setpp(int i) {
pp = i;
}
private:
int pp;
};
Now I would like to insert it into a set:

std::set<test, std::less<test my_set;
test t1;
my_set.insert(t1);

This does not work (missing a ''<'' operator) so I add the following to test:
class test {
public:
int getpp()
{
return pp;
}

void setpp(int i) {
pp = i;
}

bool operator <(test const& t) const {

return (*this).getpp() < t.getpp();
}

private:
int pp;
};

Which does not work either.

std::less implements something like:

bool operator() (T const& x, T const& y) const {
return x < y;
}
So I don''t quite see why I need to define ''<'' in ''test'' when I supply
std::less<testto my_set. It seems like double work.

By using std::less as the comparator in the set you say that you want
the objects sorted using your < operator, (since std::less will call
your < operator).

--
Erik Wikstr?m


这篇关于类型函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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