帮助模板find

编程入门 行业动态 更新时间:2024-10-11 05:25:46
本文介绍了帮助模板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>在消息中写道 news:bt ********** @ news2vision.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 home.rochester.rr/cyhome/

在文章< bt ********** @ news2vision.il>, mikets< mi **** @ frontline-pcb> ;写道:

你好, 我发现我经常使用以下结构: 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 。 -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> wrote in message news:bt**********@news2vision.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 home.rochester.rr/cyhome/

In article <bt**********@news2vision.il>, mikets <mi****@frontline-pcb> 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. -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

本文发布于:2023-11-30 14:33:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1650337.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模板   find

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!