Bonjour
I would like to use a map of strings and priority queue with a lamda based custom comparator. I wrote something similar to the code below but it does'nt compile and I believe I almost understand why (any good explainations are more than welcome)
Q: How should I modify the code below such that it compile.
auto MyComparator = [](const Person &a, const Person &b) {
return a.YearOfBirth > b.YearOfBirth;
};
std::map<std::string, std::priority_queue<Person, std::vector<Person>, decltype(MyComparator)>> Familly(MyComparator);
I did some testing before to come here and the code below code compile and works as expected but I'm not sure to fully understand why.
struct MyComparator {
bool operator()(const Person &a, const Person &b) {
return a.YearOfBirth > b.YearOfBirth;
}
};
std::map<std::string, std::priority_queue<Person, std::vector<Person>, MyComparator>> Familly;
Note: the code above also works if you use a class rather than a struct (just make sure the operator() is public)
Thaks for your help, Philippe
In
std::map<std::string, std::priority_queue<Person, std::vector<Person>, decltype(MyComparator)>> Familly(MyComparator);
you are calling ctor of std::map
.
If you just use
std::priority_queue<Person, std::vector<Person>, decltype(MyComparator)> Familly(MyComparator);
it will work.
You need to specify std::priority_queue
with lambda for every key.
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.