【问题标题】:Is it possible to make a priority queue with more than one functor? [duplicate]是否可以使用多个仿函数创建优先级队列? [复制]
【发布时间】:2020-10-09 22:45:16
【问题描述】:

如果我有一个结构和 PQ,我有办法按多个变量对堆进行排序。所以第一个年龄,然后如果它们相等,它就是高度。

Struct Person{
     int age;
     int height;
     int weight;
};

priority_queue<Person, vector<Person>, age_functor>

Struct age_functor{
     bool operator() (Person const& one, Person const& two){
          return one.age < two.age;
     }
}

代码将按年龄对队列进行排序,但我想知道是否可以以某种方式添加辅助仿函数。

【问题讨论】:

  • 只需创建一个 age_height_functor,然后按照您的描述进行比较

标签: c++ std priority-queue functor


【解决方案1】:

先比较年龄,如果年龄相等,再比较身高。

Struct age_height_functor{
     bool operator() (Person const& one, Person const& two){
          return
              one.age < two.age || // compare age
                  (one.age == two.age && // if ages are equal
                      one.height < two.height); // compare height
     }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-06-26
    • 1970-01-01
    • 2019-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-17
    • 1970-01-01
    相关资源
    最近更新 更多