How can I use lambda for container comparison operator ?

后端 未结 1 433
死守一世寂寞
死守一世寂寞 2020-12-16 04:55

This is how I would use inbuilt function or new class as a custom comparator

priority_queue< int, vector, greater > third (myints         


        
相关标签:
1条回答
  • 2020-12-16 05:11

    First define the lambda:

    auto compareFunc = [](int a, int b) { return a > b; };
    

    Then use decltype:

    typedef priority_queue<int, vector<int>, decltype(compareFunc)> q2;
    

    Now when you use q2, pass in the function:

    q2 myQueue(compareFunc);
    

    Basically, priority_queue takes the type of a function as it's 3rd template argument, while the constructor takes a pointer to that function itself.

    0 讨论(0)
提交回复
热议问题