本文介绍了如何创建一组的std ::对多数民众赞成在排序的基础上的第二::对成员使用绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我可以使用以下命令:

I know I could use the following:

template <typename Pair> 
struct ComparePairThroughSecond : public std::unary_function<Pair, bool>
{ 
    bool operator ()(const Pair& p1, const Pair& p2) const
    {  
        return p1.second < p2.second; 
    } 
};

std::set<std::pair<int, long>, ComparePairThroughSecond> somevar;

但不知道它是否可以用的boost ::绑定做

but wondered if it could be done with boost::bind

推荐答案

如何下列之一。我使用boost ::功能擦除的实际类型比较。比较是使用升压创建。纳索

How about the following one. I'm using boost::function to 'erase' the actual type of the comparator. The comparator is created using boost:bind itself.

  typedef std::pair<int, int> IntPair;
  typedef boost::function<bool (const IntPair &, const IntPair &)> Comparator;
  Comparator c = boost::bind(&IntPair::second, _1) < boost::bind(&IntPair::second, _2);
  std::set<IntPair, Comparator> s(c);

  s.insert(IntPair(5,6));
  s.insert(IntPair(3,4));
  s.insert(IntPair(1,2));
  BOOST_FOREACH(IntPair const & p, s)
  {
    std::cout << p.second;
  }

这篇关于如何创建一组的std ::对多数民众赞成在排序的基础上的第二::对成员使用绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 20:55