帮助模板find_if谓词 [英] Help with template find_if predicate

查看:35
本文介绍了帮助模板find_if谓词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


我发现我经常使用以下结构:


for(it = myvec.begin( );它!= myvec.end(); ++ it){

if(it-> name()==" myname")

break;

}


当然,方法和常量的容器可以是任何类型的



目的是使用模板仿函数并使用

std :: find_if而不是上面的循环。

我试图将某些东西与std混合: :mem_func和std :: equal。

看起来这超出了我的能力。


谢谢,


迈克。

解决方案

" mikets" < MI **** @ frontline-pcb.com>在消息中写道

news:bt ********** @ news2.netvision.net.il ...

你好,

我发现我经常使用以下结构:

for(it = myvec.begin(); it!= myvec.end(); ++ it){
if(it-> name()==" myname")
break;
}

当然,容器方法和常量可以是<任何类型。

目的是有一个模板仿函数并使用
std :: find_if而不是上面的循环。
我试图将某些东西与std混合: :mem_func和std :: equal。
看起来这超出了我的能力。

谢谢,

迈克。




这有点棘手,因为我们必须处理碰巧的各种类型

有一个名为name的方法。但它可以用模板完成:


模板< typename NAMED>

class MatchName

{

私人:

std :: string

m_name;


public:

MatchName (const NAMED& i_named):m_name(i_named.name()){}


bool

operator()(const NAMED& other)const {return other.name()== m_name;}

};


模板< typename ITER,typename NAMED>

ITER

find_by_name(ITER优先,ITER最后,const NAMED& target)

{

return std :: find_if(first,last ,MatchName< NAMED>(目标));

}


您可以这样称呼:


find_by_name (v.begin(),v.end(),Person(Melvin))


其中Person是一个名为name的方法的类。


-

Cy
http://home.rochester.rr.com/cyhome/


在文章< bt ********** @ news2.netvision.net.il>,

mikets< mi **** @ frontline-pcb.com> ;写道:

你好,

我发现我经常使用以下结构:

for(it = myvec) .begin(); it!= myvec.end(); ++ it){
if(it-> name()==" myname")
break;
}

当然,容器的方法和常量可以是任何类型的。

目的是有一个模板仿函数并使用
std :: find_if而不是上面的循环。
我试图将某些东西与std :: mem_func和std :: equal混合。
看起来这超出了我的能力。




这里有一种方法可以使用std :: tr1 :: bind:

std :: vector< A> :: iterator i =

std :: find_if



myvec.begin(),

myvec.end(),

std :: tr1 :: bind



std :: equal_to< std :: string>(),

std :: tr1 :: bind



& A :: name,

_1

),

" myn ame



);


您还可以在 www.boost.org


-Howard

mikets写道:

你好,

我发现我经常使用以下结构:

for(it = myvec.begin(); it!= myvec.end(); ++ it){
if(it-> name()==" myname")
break;
}

当然,容器的方法常量可以是任何类型的。

目的是有一个模板仿函数并使用
std :: find_if而不是上面的循环。
我试图将某些东西与std :: mem_func和std :: equal混合。
看起来这超出了我的能力。



这是一个候选谓词:

模板< class T,

class ConstType,

const ConstType(T :: * method)()>

class EqualThruMemFunc {

public:

EqualThruMemFunc(const ConstType& value):

mValue(value){}

bool operator()(T& ;实例){

返回(实例。*方法)()== mValue;

}

私人:

const ConstType& mValue;

};


class Foo {

public

// ...

const std :: string name();

};


// ...

typedef vector< Foo> MyVecType;

//

MyVecType myvec;


MyVecType :: iterator it =

find_if (myvec.begin(),

myvec.end(),

EqualThruMemFunc< MyVecType,

std :: string,

& MyVecType :: name>(" myname"));

我相信一般情况下会有更优雅的解决方案。但是这个

应该让你入门。


后来,

-

CrayzeeWulf


Hi there,

I found out that very often I use the following construct:

for (it = myvec.begin(); it != myvec.end(); ++it) {
if (it->name() == "myname")
break;
}

Certainly, the container the method and the constant can be of
any type.

The purpose is to have a template functor and use the
std::find_if instead of above loop.
I tried to mix something with std::mem_func and std::equal.
It looks that this is above my powers.

Thanks,

Mike.

解决方案

"mikets" <mi****@frontline-pcb.com> wrote in message
news:bt**********@news2.netvision.net.il...

Hi there,

I found out that very often I use the following construct:

for (it = myvec.begin(); it != myvec.end(); ++it) {
if (it->name() == "myname")
break;
}

Certainly, the container the method and the constant can be of
any type.

The purpose is to have a template functor and use the
std::find_if instead of above loop.
I tried to mix something with std::mem_func and std::equal.
It looks that this is above my powers.

Thanks,

Mike.



It''s a little tricky because we must deal with various types which happen to
have a method called "name". But it can be done with templates:

template <typename NAMED>
class MatchName
{
private:
std::string
m_name;

public:
MatchName(const NAMED &i_named) : m_name(i_named.name()) {}

bool
operator () (const NAMED &other) const {return other.name() == m_name;}
};

template <typename ITER, typename NAMED>
ITER
find_by_name(ITER first, ITER last, const NAMED &target)
{
return std::find_if(first, last, MatchName<NAMED>(target));
}

You call it like this:

find_by_name(v.begin(), v.end(), Person("Melvin"))

where "Person" is a class with a method called "name".

--
Cy
http://home.rochester.rr.com/cyhome/


In article <bt**********@news2.netvision.net.il>,
mikets <mi****@frontline-pcb.com> wrote:

Hi there,

I found out that very often I use the following construct:

for (it = myvec.begin(); it != myvec.end(); ++it) {
if (it->name() == "myname")
break;
}

Certainly, the container the method and the constant can be of
any type.

The purpose is to have a template functor and use the
std::find_if instead of above loop.
I tried to mix something with std::mem_func and std::equal.
It looks that this is above my powers.



Here''s one way to do it using std::tr1::bind:

std::vector<A>::iterator i =
std::find_if
(
myvec.begin(),
myvec.end(),
std::tr1::bind
(
std::equal_to<std::string>(),
std::tr1::bind
(
&A::name,
_1
),
"myname"
)
);

You can also find bind at www.boost.org.

-Howard


mikets wrote:

Hi there,

I found out that very often I use the following construct:

for (it = myvec.begin(); it != myvec.end(); ++it) {
if (it->name() == "myname")
break;
}

Certainly, the container the method and the constant can be of
any type.

The purpose is to have a template functor and use the
std::find_if instead of above loop.
I tried to mix something with std::mem_func and std::equal.
It looks that this is above my powers.


Here is a candidate predicate:
template < class T,
class ConstType,
const ConstType (T::*method)() >
class EqualThruMemFunc {
public:
EqualThruMemFunc( const ConstType& value ) :
mValue( value ) { }
bool operator()( T& instance ) {
return (instance.*method)() == mValue ;
}
private:
const ConstType& mValue ;
} ;

class Foo {
public
// ...
const std::string name() ;
} ;

// ...

typedef vector<Foo> MyVecType ;
//
MyVecType myvec ;

MyVecType::iterator it =
find_if( myvec.begin(),
myvec.end(),
EqualThruMemFunc<MyVecType,
std::string,
&MyVecType::name>("myname") ) ;
I am sure there is a more elegant solution for a general case. But this
should get you started.

Later,
--
CrayzeeWulf


这篇关于帮助模板find_if谓词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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