class Node {
public:
char character;
int frequency;
Node *left, *right;
};
class ComparisonClass {
public:
int operator()(Node *a, Node *b)
{
return a->frequency - b->frequency;
}
};
priority_queue<Node*, vector<Node*>, ComparisonClass> queue;
我想这样:
示例)
节点A:优先级20
节点B:优先级4
节点C:优先级6
节点D:优先级7
......
节点A(队列的第1个):优先级20
节点D(队列的第2个):优先级7
节点C:优先级6
节点B:优先级4
这样,我想按降序排列优先顺序。
但错误 - &gt;表达式:无效的运算符&lt;
我无法解决这个问题。
我应该如何修复此代码?
这是我的整体代码
class Node {
public:
char character;
int frequency;
Node *left, *right;
};
class ComparisonClass {
public:
int operator()(Node *a, Node *b)
{
return a->frequency - b->frequency;
}
};
class huffman {
public:
priority_queue<Node*, vector<Node*>, ComparisonClass> queue;
hash_map<char, string> *idc = new hash_map<char,string>();
/*
Node* huffmanCoding(int n) // incomplete code
{
for (int i = 0; i < n - 1; i++)
{
Node *z = new Node();
z->right = queue.top();
queue.pop();
z->left = queue.top();
queue.pop();
z->frequency = z->right->frequency + z->left->frequency;
queue.push(z);
}
return queue.top();
}
*/
};
int main()
{
string str = "AAAAAAABBCCCDEEEEFFFFFFG";
hash_map<char, int> dictionary;
for (int i = 0; i < str.length(); i++)
{
char temp = str.at(i);
if (dictionary.find(temp) != dictionary.end())
dictionary.find(temp)->second++;
else
dictionary.insert(hash_map<char,int>::value_type(temp,1));
}
huffman *huf = new huffman();
int number = 0;
for (hash_map<char, int>::iterator iter = dictionary.begin();
iter != dictionary.end(); ++iter)
{
Node *temp = new Node();
temp->character = iter->first;
temp->frequency = iter->second;
huf->queue.push(temp); <- error point
number++;
}
}
答案 0 :(得分:2)
您的比较器不正确。它必须返回布尔值。这应该有效:
class ComparisonClass
{
public:
bool operator()(Node *a, Node *b)
{
return a->frequency < b->frequency;
}
};