优先级队列成员函数不起作用

时间:2015-04-21 23:30:44

标签: c++ c++11 queue priority-queue

所以这是我的代码:

#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

每次推送,弹出和顶部都会出错。它说:

  

&#34; a7.hpp:49:3:错误:请求成员'推送'   'queue&lt; __ gnu_cxx :: __ normal_iterator&gt; &GT;”,   这是非类型的'std :: priority_queue

     
    

(compare(*)(),std :: vector,std :: allocator&gt;&gt;)'&#34;

  

1 个答案:

答案 0 :(得分:0)

它被称为most vexing parse

std::priority_queue< bnode<symbol> > queue(compare(),std::vector<bnode<symbol> >);

您在上面声明的是一个名为queue的函数,它有两个参数并按值返回std::priority_queue< bnode<symbol> >

第一个参数的类型是指向不带参数的函数的指针,并按值返回comparecompare(*)()

第二个参数的类型是std::vector<bnode<symbol> >


你想要的是

std::priority_queue<bnode<symbol>, std::vector<bnode<symbol> >, compare> queue;

在这种情况下,不需要传递比较器的实例,这将由priority_queue实例完成。


假设您的比较器不是默认构造的,因此您需要将实例传递给queue。然后你可以使用额外的括号来消除歧义。

std::priority_queue<bnode<symbol>, std::vector<bnode<symbol> >, compare> 
    queue((compare()), std::vector<bnode<symbol> >());
//        ^         ^

或者,如果你有一个C ++ 11编译器,你可以使用大括号而不是括号。

std::priority_queue<bnode<symbol>, std::vector<bnode<symbol>>, compare> 
    queue{compare{}, std::vector<bnode<symbol>>{}};