简体   繁体   中英

std::priority_queue: Custom ordering without defining comparator class

I want to have a priority queue with custom ordering, but lazy as I am, I don't want to define a comparator class implementing operator().

I really would like something like this to compile:

std::priority_queue<int, std::vector<int>, 
    boost::bind(some_function, _1, _2, obj1, obj2)> queue;

where some_function is a bool returning function taking four arguments, the first and second being ints of the queue, and the two last ones some objects needed for calculating the ordering (const references).

(error: 'boost::bind' cannot appear in a constant-expression)

But this doesn't compile. Even a more simple

std::priority_queue<int, std::vector<int>, &compare> queue;

won't compile, with compare being a binary function returning bool.

(error: type/value mismatch at argument 3 in template parameter list for 'template class std::priority_queue'; expected a type, got 'compare')

Any suggestions?

This could work:

std::priority_queue<int, std::vector<int>, 
    boost::function<bool(int,int)> >

Then pass in your bind expression to the queue's constructor.

PS you're getting those compilation errors because you're putting runtime-evaluated expressions where a typename or constant expression are expected.

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