c – 如何仅使用粘合剂在地图中查找值
发布时间:2020-12-16 09:25:14 所属栏目:百科 来源:网络整理
导读:在地图的第二个值中搜索我使用如下内容: typedef std::mapint,int CMyList;static CMyList myList;templateclass t struct second_equal{ typename typedef t::mapped_type mapped_type; typename typedef t::value_type value_type; second_equal(mapped_t
在地图的第二个值中搜索我使用如下内容:
typedef std::map<int,int> CMyList; static CMyList myList; template<class t> struct second_equal { typename typedef t::mapped_type mapped_type; typename typedef t::value_type value_type; second_equal(mapped_type f) : v(f) {}; bool operator()(const value_type &a) { return a.second == v;}; mapped_type v; }; ... int i = 7; CMyList::iterator it = std::find_if(myList.begin(),myList.end(),second_equal<CMyList>(i)); 问题:如何在不提供自写模板的情况下在一行中进行此类查找? 解决方法
使用选择器从map_type中选择第一个或第二个元素.
使用绑定器将值(i)绑定到std :: equal_to函数的一个参数. 使用composer将selector的输出用作equal_to函数的另一个参数. //stl version CMyList::iterator it = std::find_if( myList.begin(),std::compose1( std::bind2nd(equal_to<CMyList::mapped_type>(),i),std::select2nd<CMyList::value_type>())) ; //Boost.Lambda or Boost.Bind version CMyList::iterator it = std::find_if( myList.begin(),bind( &CMyList::mapped_type::second,_1)==i); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |