小编Zac*_*ach的帖子

优先级队列未排序

我试图实现我自己的霍夫曼编码算法和C++ STL的优先级队列似乎没有正常工作.我从字符串中获取字符并按字符串中频率的顺序将它们插入到优先级队列中.代码编译并运行没有错误,唯一的事情是树似乎没有正确排序.这是代码,

class Node {
 public:
  int freq;
  char data;
  Node(int &f, char &d) { freq=f; data=d; }
  bool operator<(const Node* &n) const { return n->freq < this->freq; }
};

void Init(priority_queue<Node*> &tree, string input) {
 map<char,int> probability;
 for(int i=0 ; i<input.size() ; i++) {
   probability[input[i]]++;
 }
 map<char,int>::iterator it = probability.begin();
 for(it ; it != probability.end() ; it++) {
   Node* blah = new Node(it->second, (char&) it->first);
   tree.push(blah);
 }
Run Code Online (Sandbox Code Playgroud)

}

我究竟做错了什么?

谢谢

c++ stl

1
推荐指数
1
解决办法
2363
查看次数

标签 统计

c++ ×1

stl ×1