如何将具有超过 2 个参数的比较函数与 c++ 一起使用优先队列?
我有一个存储数据库记录的对象类。还有一个比较类可以接受其中两个记录和一个描述如何进行排序的附加对象。我需要使用这两个类和优先级队列来合并记录。据我所知,我只能给出一个比较函数,该函数接受优先级队列的两个参数。将这个 3 参数比较器与优先级队列一起使用的最佳方法是什么?
priority_queue <Record, vector<Record>, Comparison(RecordA, RecordB, SortOrderObject)> pq;
I have an object class that stores a database record. There is also a comparison class that can take in two of these records and an additional object that describes how the ordering should be done. I need to use these two classes with a priority queue to merge the records. From what I can tell, I can only give a comparison function that takes 2 arguments to the priority_queue. What is the best way to go about using this 3 argument comparator with the priority queue?
priority_queue <Record, vector<Record>, Comparison(RecordA, RecordB, SortOrderObject)> pq;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
boost::bind
、std::bind
或手动创建一个包含额外信息的函数对象,然后将该对象传递到优先级队列中。这是一个手动版本:然后将
my_compare
作为模板参数传递给priority_queue
,并传递my_compare(sort_order)
作为队列中的比较器构造函数。Make a function object containing the extra information, either using
boost::bind
,std::bind
, or by hand, then pass that object into the priority queue. Here's a by-hand version:Then pass
my_compare
as the template argument topriority_queue
, and passmy_compare(sort_order)
as the comparator in the queue's constructor.一种方法是使 Comparison 成为一个以 SortOrderObject 作为参数的模板。
One way would to make the Comparison a template that takes the SortOrderObject as a parameter.