【发布时间】:2015-04-21 23:30:43
【问题描述】:
这是我的代码:
#ifndef SYMBOL_HPP
#define SYMBOL_HPP
struct symbol {
explicit symbol(char av = 0, int ac = 0) : value(av), count(ac) { }
char value; // actual symbol, by default 0 (empty)
int count; // count of the symbol, by default 0
}; // symbol
// compare two symbols
// symbol with a lower count is "less than" symbol with a higher count
inline bool operator<(const symbol& lhs, const symbol& rhs) {
return ((lhs.count < rhs.count) || (!(rhs.count < lhs.count) && (lhs.value < rhs.value)));
} // operator<
template <typename T> struct bnode {
explicit bnode(const T& t = T(), bnode* l = 0, bnode* r = 0)
: value(t), left(l), right(r) { }
T value; // payload
bnode* left; // left child
bnode* right; // right child
}; // struct bnode
#endif // SYMBOL_HPP
#ifndef A7_HPP
#define A7_HPP
#include <iostream>
#include <ostream>
#include "symbol.hpp"
#include <vector>
#include <queue>
#include <algorithm>
#include <map>
struct compare{
bool operator()(bnode<symbol>& a, bnode<symbol>& b){
symbol s = a.value;
symbol s1 = b.value;
return s<s1;
}
};
template <typename Iter>
bnode<symbol>* huffman_tree(Iter first, Iter last){
bnode<symbol>* root = new bnode<symbol>;
//std::priority_queue<bnode<symbol>, std::vector<bnode<symbol> >, compare() > queue;
std::priority_queue< bnode<symbol> > queue(compare(),std::vector<bnode<symbol> >);
//std::priority_queue<bnode<symbol> > queue (compare());
//std::queue<bnode<symbol> > queue;
//std::vector<symbol> symbols;
for(;first!=last;first++){
bnode<symbol>* empty;
bnode<symbol> node(*first,empty,empty);
queue.push(node);
}
while(queue.size()>1){
bnode<symbol>* left ;
*left = queue.top();
queue.pop();
bnode<symbol>* right;
*left = queue.top();
queue.pop();
char c = '0';
int l = (*left).value.count;
int r = (*right).value.count;
int total = l+r;
symbol s (c,total);
bnode<symbol> node(s,left,right);
queue.push(node);
}
bnode<symbol>* a;
*a = queue.top();
root = a;
return root;
}
// IMPLEMENT YOUR FUNCTION release_tree
void release_tree(bnode<symbol>* root){
delete root;
}
#endif // A7_HPP
每次执行 push、pop 和 top 操作时都会出错。它说:
"a7.hpp:49:3: error: 请求成员‘push’ in ‘队列<:__normal_iterator>>’, 属于非类类型‘std::priority_queue
(比较 (*)(), std::vector, std::allocator > >)'"
【问题讨论】:
-
欢迎来到 StackOverflow!由于您是新来的,而且我现在已经回答了您的两个问题,但没有任何迹象表明这些答案是否对您有所帮助,所以我会指给您this help topic。
标签: c++ c++11 queue priority-queue