[英]C++ How to pass a parameter to an STL function
我必须将无序的 map 传递给优先级队列的比较器 function,并使用链接将参数传递给比较 function? 我决定按如下方式进行:
priority_queue < int, std::vector<int>, compare(freq) > pq;
struct compare
{
compare( std::unordered_map<int,int>& freq1 )
{
freq = freq1;
}
bool operator()( int& el1, int& el2 ){
return freq[el1] < freq[el2];
}
std::unordered_map<int,int> freq;
};
但是我收到错误:
Template argument for template type parameter must be a type
我究竟做错了什么?
如错误消息所述, compare(freq)
不是类型,不能将其指定为类型模板参数。
您应该将compare(freq)
指定为priority_queue
的构造函数的参数,并将compare
指定为类型模板参数。
priority_queue < int, std::vector<int>, compare> pq{compare(freq)};
// ^^^^^^^ ^^^^^^^^^^^^^^^
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.