C++ STL:使用带有priority_queue的map

发布于 2024-10-08 12:57:54 字数 648 浏览 0 评论 0 原文

我试图通过将字母及其相应的值保存到映射中,然后将映射插入到优先级队列中来实现霍夫曼编码。当我尝试声明我的队列时,出现参数转换错误。我到底应该输入什么作为参数?我这里所拥有的是我最好的猜测。

void main()
{
 ifstream doc("doc.txt"); 
 map<char, int> C;
 char letter;
 while(!doc.eof()){
  doc.get(letter);
  if(letter >= 'a' && letter <= 'z')
   C[letter]++;
 }
 priority_queue<int, map<char,int>, greater<int> > Q(C); //also tried greater<map<char,int>>
 /*map<char, int>::const_iterator it;
 for(it = C.begin(); it != C.end(); it++)
  cout<<it->first<<" "<<it->second<<endl;*/
}

我觉得问这个问题有点愚蠢,但彻底的谷歌搜索并没有给我答案。非常感谢您的帮助!

I'm trying to implement Huffman coding by saving letters and their corresponding values into a map then inserting the map into a priority queue. I am getting a parameter conversion error when I try to declare my queue. What exactly am I supposed to put as the parameters? What I have here was my best guess.

void main()
{
 ifstream doc("doc.txt"); 
 map<char, int> C;
 char letter;
 while(!doc.eof()){
  doc.get(letter);
  if(letter >= 'a' && letter <= 'z')
   C[letter]++;
 }
 priority_queue<int, map<char,int>, greater<int> > Q(C); //also tried greater<map<char,int>>
 /*map<char, int>::const_iterator it;
 for(it = C.begin(); it != C.end(); it++)
  cout<<it->first<<" "<<it->second<<endl;*/
}

I feel kind of dumb asking this but thorough googling did not get me the answer. Thanks a lot for the help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

白况 2024-10-15 12:57:54

您不能使用映射作为priority_queue的底层容器:priority_queue必须可以自由地对容器中的内容进行重新排序,这对于映射来说是不允许的。只能使用向量和双端队列(来自标准容器)。

因此,容器类型将类似于 vector >。然后,您需要一个较小/较大的运算,仅考虑该对的第二个字段。

You cannot use a map as the underlying container for a priority_queue: the priority_queue must be free to reorder things in the container, which is not allowed for maps. Only vector and deque can be used (from the standard containers).

So, the container type would be something like vector<pair<char, int> >. Then, you need a less/greater operation that only takes the second field of the pair into account.

臻嫒无言 2024-10-15 12:57:54

方法 1,使用 函子

#include <unordered_map>
#include <vector>
#include <iostream>
#include <queue>
using namespace std;

typedef pair<char, int> PAIR;

int main(int argc, char *argv[]) {
    unordered_map<char, int> dict = {{'a', 12}, {'b', 9}, {'c', 7}, {'d', 10},};
    struct cmp {
        bool operator()(const PAIR &a, const PAIR &b) {
            return a.second < b.second;
        };
    };
    priority_queue<PAIR, vector<PAIR>, cmp> pq(dict.begin(), dict.end());
    while (!pq.empty()) {
        PAIR top = pq.top();
        cout << top.first << " - " << top.second << endl;
        pq.pop();
    }
}

方法 2,使用函数和 decltype()

auto cmp = [](const PAIR &a, const PAIR &b) {
    return a.second < b.second;
};
priority_queue<PAIR, vector<PAIR>, decltype(cmp)> pq(
        dict.begin(), dict.end(), cmp);

上面例子中的priority_queue是按值排序的,

a - 12
d - 10
b - 9
c - 7

method 1, using a functor,

#include <unordered_map>
#include <vector>
#include <iostream>
#include <queue>
using namespace std;

typedef pair<char, int> PAIR;

int main(int argc, char *argv[]) {
    unordered_map<char, int> dict = {{'a', 12}, {'b', 9}, {'c', 7}, {'d', 10},};
    struct cmp {
        bool operator()(const PAIR &a, const PAIR &b) {
            return a.second < b.second;
        };
    };
    priority_queue<PAIR, vector<PAIR>, cmp> pq(dict.begin(), dict.end());
    while (!pq.empty()) {
        PAIR top = pq.top();
        cout << top.first << " - " << top.second << endl;
        pq.pop();
    }
}

method 2, using a function and decltype() it

auto cmp = [](const PAIR &a, const PAIR &b) {
    return a.second < b.second;
};
priority_queue<PAIR, vector<PAIR>, decltype(cmp)> pq(
        dict.begin(), dict.end(), cmp);

the priority_queue in above example is sorted by value,

a - 12
d - 10
b - 9
c - 7
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文