问题是什么?
当我使用 STL 的优先级队列时,我想使用最小堆,所以我使用如下代码。
它适用于默认选项,但不适用于“更大的选项”。
它总是像上图一样排列。我完全不知道为什么会这样。
struct node {
string code;
int fre;
bool operator<(const node& rhs) const {
return fre < rhs.fre;
}
bool operator>(const node& rhs) const {
return fre > rhs.fre;
}
};
std::priority_queue<node, vector<node>, greater<node>> q;
std::map<node,int> huffman_tree;
int main(void)
{
int f;
for (int i = 1; i <= n; i++) {
string c;
std::cin >> c >> f;
node huffman = { c,f };
q.push(huffman);
}
q.pop();
return 0;
}