so here is my code:
#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
I get errors everytime i do push, pop, and top. It says
a7.hpp:49:3: error: request for member ‘push’ in ‘queue<__gnu_cxx::__normal_iterator > >’, which is of non-class type ‘std::priority_queue
(compare (*)(), std::vector, std::allocator > >)’
any thoughts? thanks in advance!
Aucun commentaire:
Enregistrer un commentaire