【问题标题】:Priority queue member functions are not working优先队列成员函数不起作用
【发布时间】: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


【解决方案1】:

它被称为most vexing parse

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

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

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

第二个参数的类型是std::vector&lt;bnode&lt;symbol&gt; &gt;


你想要的是

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>>{}};

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-14
    • 2016-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多