STL之Set自定义排序

来源:互联网 发布:下载cad制图软件 编辑:程序博客网 时间:2024/05/11 20:01
方法一、以类为比较器

struct classCompare
{
   bool operator()(const int& lhs, const int& rhs)
   {
       return lhs < rhs ;
   }
};
int main(void)
{
  set<int, classCompare> aSet ;
  system("pause") ;
  return 0 ;
}

方法二、以指针函数为比较器

bool fncmp(int lhs, int rhs)
{
  return lhs < rhs ;
}
int main(void)
{
  bool(*fn_pt)(int, int) = fncmp ;
  set<int, bool(*)(int, int)> aSet(fn_pt) ;
  system("pause") ;
  return 0 ;
}

方法三、在类定义里面重载operator算子(略)

采用第一种吧。
1 1