简体   繁体   English

用类对象对双端队列进行排序

[英]Sorting a deque with class objects

I have a question about sorting a deque with class objects. 我有一个关于使用类对象对双端队列进行排序的问题。 So I have one deque called "rq" which holds, multiple types of data. 因此,我拥有一个称为“ rq”的双端队列,该双端队列可以存储多种类型的数据。 I want to sort the deque by comparing the TAU values. 我想通过比较TAU值对双端队列进行排序。 However, I keep getting insane compiler errors when trying to sort the deque. 但是,在尝试对双端队列进行排序时,我总是收到疯狂的编译器错误。 Below is the the function I tried to do in order to compare TAU values and sort the deque. 下面是我为了比较TAU值并对双端队列排序而尝试执行的功能。 How would i go about solving this problem? 我将如何解决这个问题?

 deque<system>rq 
 //and so on
 struct tauSort
 {
     bool operator ()( system &a,  system  &b)
     {              
         return a.getTau() < b.getTau();        
     } 
 }

 //Blah Blah
 system sorting;
 sort(rq.begin(), rq.end(), sorting)

This: 这个:

system sorting;

should be this: 应该是这样的:

tauSort sorting;

because you want to pass the sorting functor, not a system object. 因为您要传递排序函子,而不是system对象。 And you are forgetting a semicolon at the end of the sort statement too. 而且您也忘记了sort语句末尾的分号。

You can also simply do: 您还可以简单地执行以下操作:

sort(rq.begin(), rq.end(), tauSort());

Not to mention that you can pass the system s by constant reference in your function, since you don't need to modify them. 更不用说您可以在函数中通过常量引用传递system了,因为您不需要修改它们。 And it's better for you to make the operator() const as well: 而且最好也将operator()设为const:

bool operator ()(system const& a,  system const& b) const { … }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM