如何初始化std :: set比较器? [英] How do I initialize a std::set comparator?

查看:99
本文介绍了如何初始化std :: set比较器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要基于std :: set用另一个类Object的某个对象o初始化一些新数据类型TType的比较器:

I need to initialize some comparator of the new data type TType based on std::set with some object o of another class Object:

typedef std::set <unsigned int, sortSet(o)> TType

此声明在类的旁边(在头文件中)。在声明时此对象不必存在,它将在以后创建。

This declaration is otside the class (in header file). At the time of the declaration this object does not have to exist, it will be created later.

class sortSet
{
   private:
      Object o;

   public:
      sortSet(Object &oo): o(oo) {}
      bool operator() ( const unsigned int &i1, const unsigned int &i2 ) const
      {
         //Some code...
      }
};

如果声明位于某个方法内(当对象已经创建时),情况将非常简单...我该怎么办?

If the declaration was inside some method (when object has already been created), situation would be quite simple... What can I do?

推荐答案

template参数必须是该参数的 type 比较器,而不是该类型的特定对象;您可以提供要在 std :: set 构造函数中使用的特定比较器对象:

The template parameter needs to be the type of the comparator, not a specific object of that type; you can provide a specific comparator object to be used in the std::set constructor:

typedef std::set<unsigned int, sortSet> TType;

Object o;
TType mySet(sortSet(o));

这篇关于如何初始化std :: set比较器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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