STL排序问题 [英] STL sort question

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

问题描述

我想用STL排序从std :: vector派生的类:

template< typename T,typename fitParaType,typename fitResType>

class Manipulator {

//我现在不能使用

来访问私人会员//比较?

朋友班比较;

public:

...

void sort(人口< Genome< T,fitParaType>>& pop);

private:

...

const Functor< fitParaType,fitResType> * _func;


class比较{

public:

比较(){}

bool operator()(基因组< T,fitParaType>& arg1,

Genome< T,fitParaType>& arg2)

{

return(_func-> value(arg1.getParams())

< _func-> value(arg2.getParams()));

}

};

};


模板< typename T,typename fitParaType,typename fitResType>

void操纵器< T,适合ParaType,fitResType> :: sort(Population< Genome< T,

fitParaType> >&安培;流行)

{

//人口来自std :: vector

std :: sort(pop.begin(),pop。 end(),Manipulator< T,fitParaType,

fitResType> :: Compare());

}

//实例化,所以它编译

模板类操纵器< RealGene< double>,double,double> ;;


我正在尝试比较朋友类的操纵器,所以我可以使用

_func函数。但是,我得到:

/Manipulator.cpp:35:从这里实例化

... / Manipulator.hpp:64:错误:''类操纵器< RealGene< ; double>,double,

double> :: Compare''没有名为''_func'的成员'


如何编写一个Compare类,我可以用来排序吗?有没有比我一直尝试的更好的方式?


谢谢,


Paul Schneider

解决方案



" Paul Schneider" < PA ******* @ uboot.com>在消息中写道

新闻:c6 ********* @ bird.wu-wien.ac.at ...

我想排序一个班级源自STD排序的std :: vector:

模板< typename T,typename fitParaType,typename fitResType>
类操纵器{
//我现在不应该能够访问私人会员
//比较?
朋友类比较;
公开:
...
[snip]
我正在尝试make比较朋友类的Manipulator,所以我可以使用_func函数。但是,我得到了:
/Manipulator.cpp:35:从这里实例化
../Manipulator.hpp:64:错误:''类操纵器< RealGene< double>,double,
double> :: Compare''没有名为''_func'的成员'

如何编写一个可用于排序的Compare类?有没有比我一直尝试的更好的方式?

谢谢,

Paul Schneider




这是一个棘手的领域,我认为最近标准已经改变了关于内部阶级和友谊的标准。但是以下可能更有可能工作

模板< typename T,typename fitParaType,typename fitResType>

类操纵器{

类比较;

朋友类比较;


如果没有前向声明,编译器认为朋友类

比较是指在Manipulator之外声明的类,而不是您稍后声明的内部

类。


john


Paul Schneider写道:

我想用STL排序从std :: vector派生的类:

template< typename T, typename fitParaType,typename fitResType>
类操作符{
//我现在不能使用
//比较来访问私有成员吗?
朋友类比较;


我不确定,但我认为那应该是朋友班

Manipulator :: Compare;,以免转发-declare一个类在
名称空间范围内。但这不是你最大的问题。

public:
...
void sort(Population< Genome< T,fitParaType>>& pop);
私人:
...
const Functor< fitParaType,fitResType> * _func;

类比较{
public:
比较( ){}
bool operator()(Genome< T,fitParaType>& arg1,
Genome< T,fitParaType>& arg2)
{
return(_func-> ; value(arg1.getParams())
< _func-> value(arg2.getParams()));


Manipulator :: Compare对象与Manipulator

对象完全不同。你只能通过Manipulator对象访问Manipulator的非静态成员_func


也许Compare对象应该包含自己的_func副本,或者a

对创建Compare对象的Manipulator对象的引用。


我不知道_func的作用。你确定它定义了std :: sort所要求的严格弱的

顺序吗?比较向量的一种常用方法

按字典顺序排列 - 实际上很常见,你可以比较两个

std :: vectors with<或者std :: less。

}
};
};

模板< typename T,typename fitParaType,typename fitResType>
void Manipulator< T,fitParaType,fitResType> :: sort(Population< Genome< T,
fitParaType>>& pop)
//
//群体来源于std :: vector


你可能应该使用委托而不是继承,但是你的电话是



std :: sort( pop.begin(),pop.end(),Manipulator< T,fitParaType,
fitResType> :: Compare());
}

//实例化,所以它编译
模板类操纵器< RealGene< double>,double,double> ;;


你确定这是必要的吗?您可能不应该单独编译

模板定义。在

常见问题解答中有一个关于此的条目。

我正在尝试比较朋友类的操纵器,所以我可以使用
_func函数。但是,我得到了:
/Manipulator.cpp:35:从这里实例化
../Manipulator.hpp:64:错误:''类操纵器< RealGene< double>,double,
double> :: Compare''没有名为''_func'的成员'
如何编写一个可用于排序的Compare类?有没有比我尝试过的方式更好的方法?



-

问候,

巴斯特。


John Harrison写道:

" Paul Schneider" < PA ******* @ uboot.com>在消息中写道
新闻:c6 ********* @ bird.wu-wien.ac.at ...

我想排序一个班级源自STD排序的std :: vector:

模板< typename T,typename fitParaType,typename fitResType>
类操纵器{
//我现在不应该能够访问私人会员
//比较?
朋友类比较;
公开:
...



[snip]

我正在尝试比较朋友类的操纵器,所以我可以使用_func函数。但是,我得到了:
/Manipulator.cpp:35:从这里实例化
../Manipulator.hpp:64:错误:''类操纵器< RealGene< double>,double,
double> :: Compare''没有名为''_func'的成员'

如何编写一个可用于排序的Compare类?有没有比我尝试过的方式更好的方式?

谢谢,

Paul Schneider



这个是一个棘手的领域,我认为最近有关内部阶级和友谊的标准已经改变了。但是以下可能更有可能工作

模板< typename T,typename fitParaType,typename fitResType>
类操纵器{
类比较;
朋友类比较;

没有前向声明编译器认为朋友类
比较是指在Manipulator之外声明的类,而不是您稍后声明的内部类。 />
john



谢谢,在尝试了你的建议之后,朋友的声明仍然没有工作。 (指针被排除在外吗?)。现在我尝试了以下比较课程:


class比较{

public:

比较(const Functor< fitParaType,fitResType> * func):_ func(func){}

bool operator()(Genome< T,fitParaType>& arg1,

Genome< T,fitParaType>& arg2 )

{

return(_func-> value(arg1.getParams())

< _func-> value(arg2。 getParams()));

}

private:

const Functor< fitParaType,fitResType> * _func;
$ b $实现sort函数的



模板< typename T,typename fitParaType,typename fitResType>

void Manipulator< T,fitParaType,fitResType> :: sort(Population< Genome< T,

fitParaType>>& pop)

{

std :: sort(pop.begin(),pop.end(),this-> Compare(_func));

}


这只给我一个错误:

... / Manipulator.cpp:32:错误:调用非函数`类

操纵器< RealGene< double>,double,double> ::比较''


如何摆脱这个?


再次感谢,


Paul


I want to sort a class derived from std::vector with STL sort.:
template<typename T, typename fitParaType, typename fitResType>
class Manipulator{
// shouldn''t I now be able to access private members with
// Compare?
friend class Compare;
public:
...
void sort(Population<Genome<T, fitParaType> >& pop);
private:
...
const Functor<fitParaType, fitResType>* _func;

class Compare{
public:
Compare(){}
bool operator () (Genome<T, fitParaType>& arg1,
Genome<T, fitParaType>& arg2)
{
return (_func->value(arg1.getParams())
< _func->value(arg2.getParams()));
}
};
};

template<typename T, typename fitParaType, typename fitResType>
void Manipulator<T, fitParaType, fitResType>::sort(Population<Genome<T,
fitParaType> >& pop)
{
// Population is derived from std::vector
std::sort(pop.begin(), pop.end(), Manipulator<T, fitParaType,
fitResType>::Compare() );
}
// instantiation, so it compiles
template class Manipulator<RealGene<double>, double, double>;

I am trying to make Compare a friend class of Manipulator, so I can use
the _func function. However, I am getting:
/Manipulator.cpp:35: instantiated from here
.../Manipulator.hpp:64: error: ''class Manipulator<RealGene<double>, double,
double>::Compare'' has no member named ''_func''

How can I write a Compare class that I can use for sorting? Is there a
better way than the way I have been trying?

Thanks,

Paul Schneider

解决方案


"Paul Schneider" <pa*******@uboot.com> wrote in message
news:c6*********@bird.wu-wien.ac.at...

I want to sort a class derived from std::vector with STL sort.:
template<typename T, typename fitParaType, typename fitResType>
class Manipulator{
// shouldn''t I now be able to access private members with
// Compare?
friend class Compare;
public:
... [snip]
I am trying to make Compare a friend class of Manipulator, so I can use
the _func function. However, I am getting:
/Manipulator.cpp:35: instantiated from here
../Manipulator.hpp:64: error: ''class Manipulator<RealGene<double>, double,
double>::Compare'' has no member named ''_func''

How can I write a Compare class that I can use for sorting? Is there a
better way than the way I have been trying?

Thanks,

Paul Schneider



This is a tricky area, and I think the standard has changed recently
concerning inner classes and friendship. However the following is probably
more likely to work

template<typename T, typename fitParaType, typename fitResType>
class Manipulator{
class Compare;
friend class Compare;

Without the forward declaration the compiler thinks that friend class
Compare refers to a class declared outside of Manipulator, not the inner
class you declare later.

john


Paul Schneider wrote:

I want to sort a class derived from std::vector with STL sort.:
template<typename T, typename fitParaType, typename fitResType>
class Manipulator{
// shouldn''t I now be able to access private members with
// Compare?
friend class Compare;
I''m not sure, but I think that should be "friend class
Manipulator::Compare;", so as not to forward-declare a class at
namespace scope. That''s not your biggest problem, though.
public:
...
void sort(Population<Genome<T, fitParaType> >& pop);
private:
...
const Functor<fitParaType, fitResType>* _func;

class Compare{
public:
Compare(){}
bool operator () (Genome<T, fitParaType>& arg1,
Genome<T, fitParaType>& arg2)
{
return (_func->value(arg1.getParams())
< _func->value(arg2.getParams()));
A Manipulator::Compare object is quite different from a Manipulator
object. You can only access the non-static member _func of Manipulator
through a Manipulator object.

Perhaps the Compare object should contain its own copy of _func, or a
reference to the Manipulator object which created the Compare object.

I don''t know what _func does. Are you sure it defines a strict weak
ordering, as required by std::sort? A common way to compare vectors
is lexicographically -- so common, in fact, that you can compare two
std::vectors with < or std::less.
}
};
};

template<typename T, typename fitParaType, typename fitResType>
void Manipulator<T, fitParaType, fitResType>::sort(Population<Genome<T,
fitParaType> >& pop)
{
// Population is derived from std::vector
You should probably using delegation rather than inheritance, but it''s
your call.
std::sort(pop.begin(), pop.end(), Manipulator<T, fitParaType,
fitResType>::Compare() );
}
// instantiation, so it compiles
template class Manipulator<RealGene<double>, double, double>;
Are you sure that''s necessary? You should probably not be compiling the
template definitions separately. There is an entry about this in the
FAQ.
I am trying to make Compare a friend class of Manipulator, so I can use
the _func function. However, I am getting:
/Manipulator.cpp:35: instantiated from here
../Manipulator.hpp:64: error: ''class Manipulator<RealGene<double>, double,
double>::Compare'' has no member named ''_func''
How can I write a Compare class that I can use for sorting? Is there a
better way than the way I have been trying?


--
Regards,
Buster.


John Harrison wrote:

"Paul Schneider" <pa*******@uboot.com> wrote in message
news:c6*********@bird.wu-wien.ac.at...

I want to sort a class derived from std::vector with STL sort.:
template<typename T, typename fitParaType, typename fitResType>
class Manipulator{
// shouldn''t I now be able to access private members with
// Compare?
friend class Compare;
public:
...



[snip]

I am trying to make Compare a friend class of Manipulator, so I can use
the _func function. However, I am getting:
/Manipulator.cpp:35: instantiated from here
../Manipulator.hpp:64: error: ''class Manipulator<RealGene<double>, double,
double>::Compare'' has no member named ''_func''

How can I write a Compare class that I can use for sorting? Is there a
better way than the way I have been trying?

Thanks,

Paul Schneider


This is a tricky area, and I think the standard has changed recently
concerning inner classes and friendship. However the following is probably
more likely to work

template<typename T, typename fitParaType, typename fitResType>
class Manipulator{
class Compare;
friend class Compare;

Without the forward declaration the compiler thinks that friend class
Compare refers to a class declared outside of Manipulator, not the inner
class you declare later.

john


Thanks, after trying your suggestion the friend statement still doesn''t
work. (Are pointers excluded?). Now I tried the following Compare class:

class Compare{
public:
Compare(const Functor<fitParaType, fitResType>* func) : _func(func){}
bool operator () (Genome<T, fitParaType>& arg1,
Genome<T, fitParaType>& arg2)
{
return (_func->value(arg1.getParams())
< _func->value(arg2.getParams()));
}
private:
const Functor<fitParaType, fitResType>* _func;
};

with implementation of the sort function:

template<typename T, typename fitParaType, typename fitResType>
void Manipulator<T, fitParaType, fitResType>::sort(Population<Genome<T,
fitParaType> >& pop)
{
std::sort(pop.begin(), pop.end(), this->Compare(_func) );
}

This gives me only one error:
.../Manipulator.cpp:32: error: call to non-function `class
Manipulator<RealGene<double>, double, double>::Compare''

How can I get rid of this?

Thanks again,

Paul


这篇关于STL排序问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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