jeudi 5 août 2021

How to pass custom comparator into priority queue constructor inline within map constructor in C++?

I have a issue similar to priority_queue with custom comparator as value in map constructor error.

I would like to have a map whose value is a priority_queue with a custom comparator that requires an outside parameter.

void someFunc() {
  int outsideParam = 0;
  auto cmp = [&](int a, int b){
    // logic involving outsideParam
  };

  unordered_map<std::string, priority_queue<int, vector<int>, decltype(cmp)>> mapObj;
}

The above code won't compile. I understand I need to pass the the lambda into the priority_queue constructor similar to

priority_queue<int, vector<int>, decltype(cmp)> pq(cmp);

But I don't know how to pass it when the priority_queue is within a map.

Thanks!

Aucun commentaire:

Enregistrer un commentaire