C++ 如何将sort()的compare参数与模板和自定义数据结构一起使用

C++ 如何将sort()的compare参数与模板和自定义数据结构一起使用,c++,templates,sorting,stl,C++,Templates,Sorting,Stl,我想创建一个比较函数对象,它将帮助我对自定义数据结构的向量进行排序。由于我同时使用一个模板,我正在努力找出它应该在哪里实现以及需要的任何附加代码。下面的大部分代码都可以忽略。包含它是为了完整性,但是比较函数对象用于printsummmary()的末尾。简而言之,如何实现compare函数对象 #include<map> #include<vector> //#include<iostream> #include<algorithm> using n

我想创建一个比较函数对象,它将帮助我对自定义数据结构的向量进行排序。由于我同时使用一个模板,我正在努力找出它应该在哪里实现以及需要的任何附加代码。下面的大部分代码都可以忽略。包含它是为了完整性,但是比较函数对象用于
printsummmary()
的末尾。简而言之,如何实现compare函数对象

#include<map>
#include<vector>
//#include<iostream>
#include<algorithm>
using namespace std;

template <class T>
class Record{
   public:
   T item;
   int total;
};

   template<class T>
   bool compare(const Record<T> & a, const Record<T> & b){ //should a come before b?
      if(a.total > b.total)
         return true;
      if(a.total < b.total)
         return false;
      if(a.total == b.total){
         if(a.item < b.item)
            return true; 
         else
            return false;
      }
   }

template <class T>
class Counter{
   public:
      map<T, int> m;

   void printSummary(){

      typename map<T, int>::const_iterator itr; 
      vector< Record<T> > printlist;
      Record<T> temp;
      int i = 0;

      for( itr = m.begin(); itr != m.end(); ++itr ){
         temp.item = (*itr).first;
         temp.total = (*itr).second;
         printlist.push_back(temp);
         i++;
      }

      sort(printlist.begin(), printlist.end(), compare);

      //output sorted printlist contents
   }

};
#包括
#包括
//#包括
#包括
使用名称空间std;
模板
课堂记录{
公众:
T项;
整数合计;
};
模板
bool compare(const-Record&a,const-Record&b){//a应该在b之前吗?
如果(a.总计>b.总计)
返回true;
如果(a.总计打印列表;
记录温度;
int i=0;
对于(itr=m.begin();itr!=m.end();++itr){
温度项目=(*itr).第一;
总温度=(*itr).秒;
打印列表。推回(临时);
i++;
}
排序(printlist.begin()、printlist.end()、compare);
//输出已排序的打印列表内容
}
};
在调用
sort()
时,指定函数模板的名称,而不实例化它

sort(printlist.begin(), printlist.end(), compare);
//                                       ^^^^^^^
函数模板的裸名称表示编译器的整个重载集(其中该集包括该模板的所有可能的专门化)

为了消除歧义,您需要实例化
compare()
,以便提供一个函数的地址:

sort(printlist.begin(), printlist.end(), compare<T>);
//                                              ^^^
然后,您可以通过这种方式将其传递到
sort()
,而无需模板实例化(但您需要创建一个functor实例,尽管临时的可以,如下所示):


到目前为止,这看起来还不错,您必须仅使用类型
T

sort(printlist.begin(), printlist.end(), compare<T>);
因为在这一点上,它总是相等的。你可以把它简化为

if (a.total > b.total)
    return true;

if (a.total < b.total)
    return false;

return a.item < b.item;
if(a.total>b.total)
返回true;
如果(a.总计
问题是什么?我应该放在哪里/我应该如何编码
compare()
,这样就可以通过
sort()访问它了。
可以放在那里。将实例化传递给
sort()
时,只需指定一个实例化即可。参见我的回答对于实例化
记录
对象,可以提出同样的观点吗?我需要指定
Record
,以便编译器知道如何使用
模板
(我在示例中就是这样做的),而不是其他模板。比如说
模板
@user1905552:我想我不明白你的问题,你能重新措辞吗?我可以尝试其他问题吗。如果我使用您提到的第一个解决方案,我可以在
sort()
中使用
比较
?然后我能在
模板下写一个新的比较函数对象吗?它可能包含不同的逻辑?@user1905552:我还是不明白。如果您只是将
T
替换为
U
,这是不变的
T
U
只是
计数器
模板的类型参数的符号名称
sort(printlist.begin(), printlist.end(), compare<T>);
if(a.total == b.total){
if (a.total > b.total)
    return true;

if (a.total < b.total)
    return false;

return a.item < b.item;