问题描述
让我介绍一下我的课程:
Let me introduce my class:
#include <tuple>
#include <queue>
#include <initializer_list>
template <typename HType>
class Huffman {
class TreeOfLetters {
HType Value;
char Letter;
TreeOfLetters *Root = nullptr;
TreeOfLetters *Left = nullptr;
TreeOfLetters *Right = nullptr;
TreeOfLetters(HType _Value, char _Letter) : Value(_Value), Letter(_Letter) {
};
};
auto compare = [](const TreeOfLetters &a,const TreeOfLetters &b){
return a.Value < b.Value;};
std::priority_queue(TreeOfLetters, std::set<TreeOfLetters>, compare);
public:
Huffman(std::initializer_list<HType> _Values, std::initializer_list<char> _Chars);
Huffman(const Huffman& orig) = delete;
};
我想要实现的是在std :: priority_queue中存储指向Trees的指针,使得在顶部将是值最高的指针.
What I want to achieve is to store pointers to Trees in the std::priority_queue in such a way that on top will be the one which the value is the highest.
我知道我可以随心所欲地得到一个容器,然后对其进行排序,但是我想使用std :: priority_queue,它会自动完成它.
I know that I could get a container whatever I like and then sort it but I want to use the std::priority_queue which will do it automatically.
由于我不太熟悉c ++ 11功能,因此我收到一些错误,非常感谢您为摆脱这些错误所提供的帮助.
Since I am not very familiar with c++11 features I am receiving some errors and I would highly appreciate your help with getting rid of them.
首先,我不确定我的lambda是否正确,这就是为什么我希望一些专家确认我的approuch是否正确.
First of all I am not sure if my lambda is correct that is why I want some experts to cofirm whether my approuch is correct or not.
我收到的警告:
无法解析标识符比较.
无法解析标识符值.
非常感谢Piotr和Thoran的帮助,我确实使<运算符,所以我将这些行添加到我的课程中
Big thanks to Piotr and Thoran for their help I did overload the < operator so I add these lines to my class
bool operator<(const TreeOfLetters &a,const TreeOfLetters &b)
{
return a.Value < b.Value;
}
我仍然收到来自下一行的错误,我非常感谢您的帮助,可以帮助我解决问题.
I am still receiving an error from a following line I will highly appreciate an approuch what will help me to solve it.
std::priority_queue <TreeOfLetters, std::vector<TreeOfLetters>, Compare> queue;
推荐答案
您对Compare函数和队列的定义看起来很奇怪.你的意思是这样吗?这在Visual C ++中对我有用
Your definition of the Compare function and the queue looks strange. Did you mean something like this? This works for me in Visual C++
template <typename HType>
class Huffman {
class TreeOfLetters {
HType Value;
char Letter;
TreeOfLetters *Root;
TreeOfLetters *Left;
TreeOfLetters *Right;
TreeOfLetters(HType _Value, char _Letter) : Value(_Value), Letter(_Letter), Root(nullptr), Left(nullptr), Right(nullptr){
};
};
struct Compare
{
bool operator()(const TreeOfLetters& a, const TreeOfLetters& b)
{
return a.Value < b.value;
}
};
std::priority_queue<TreeOfLetters, std::vector<TreeOfLetters>, Compare> queue;
public:
Huffman(std::initializer_list<HType> _Values, std::initializer_list<char> _Chars) : queue(Compare()) {}
Huffman(const Huffman& orig) = delete;
};
或者您可以将比较实现为lambda
or you can implement the comparison as a lambda
std::priority_queue<TreeOfLetters, std::vector<TreeOfLetters>, [](const TreeOfLetters& a, const TreeOfLetters& b){return a.Value < b.value;}> queue;
您应该使用某些特征将HType限制为基本值,否则必须为要使用的实际HType实现>"运算符.
You should restrict your HType to basic values using some traits, or else you have to implement the ">" operator for the actual HType you want to use.
另请参见上面的@Piotr Skotnicki评论.
See also @Piotr Skotnicki comment above.
这篇关于std :: priority_queue包含我自己的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!