简体   繁体   中英

Struct ordering criteria of a priority queue in C++

I was wondering if it is possible to order two different priority_queues (stl) with the same struct but with a different ordering criteria. Imagine something like:

struct stuff {
  int a, b;
}

priority_queue<stuff> a;
priority_queue<stuff> b;

Note that this is not functional code of C++, just pseudocode. Then, I would like to know if it is possible that a and b differ in the ordering, one with the biggest stuff.a at the start of the queue and the other one with the biggest stuff.b.

Thank you very much!

Marc

Documentation on std::priority_queue has an example how to use lambda with it, so just create 2 of them:

struct stuff {
  int a, b;
};

void foo() 
{
    auto cmpBiggerA = []( const stuff &s1, const stuff &s2 ) { return s1.a > s2.a; };
    auto cmpBiggerB = []( const stuff &s1, const stuff &s2 ) { return s1.b > s2.b; };

    priority_queue<stuff,std::vector<stuff>,decltype(cmpBiggerA)> a( cmpBiggerA );
    priority_queue<stuff,std::vector<stuff>,decltype(cmpBiggerB)> b( cmpBiggerB );
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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