C++ 如何将参数传递给 STL 函数
C++ How to pass a parameter to an STL function
我不得不将无序映射传递给优先级队列的比较函数,并使用 link Passing a parameter to a comparison 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)
作为 constructor of priority_queue
的参数,并指定 compare
作为类型模板参数。
priority_queue < int, std::vector<int>, compare> pq{compare(freq)};
// ^^^^^^^ ^^^^^^^^^^^^^^^
我不得不将无序映射传递给优先级队列的比较函数,并使用 link Passing a parameter to a comparison 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)
作为 constructor of priority_queue
的参数,并指定 compare
作为类型模板参数。
priority_queue < int, std::vector<int>, compare> pq{compare(freq)};
// ^^^^^^^ ^^^^^^^^^^^^^^^