请教hash_map编程题

zyang008 2011-05-11 11:04:57
程序的目的是统计文件中每个字符串出现的频率,然后用优先队列来提取前10个频率最高的字符串输出。
程序能跑,但是结果有问题

输出的总是文件里最后一个字符串(它不是频率最高的),频率显示是这个文件里字符串的总数


#include <stdio.h>
#include <stdlib.h>
#include <queue>
#include <hash_map>
#include <string>
//#include <map>
using namespace std;
struct cmpQue{
bool operator()(pair<char*,int>p1,pair<char*,int>p2){
return stricmp(p1.first,p2.first)<0?true:false;
}
};
/*
struct cmpHash{
bool operator()(char* s1,char* s2){
return stricmp(s1,s1)==0;
}
};*/
//const char*, int, hash<const char*>, cmp
int main()
{
FILE *fin;
hash_map<char*,int> hmap;
priority_queue<pair<char*,int>,vector<pair<char*,int> >,cmpQue> hque;
fin=fopen("1.txt","r");
char word[20];
/*
strcpy(word[0],"123");
hmap[word[0]]++;
hmap[word[0]]++;
strcpy(word[1],"2");
hmap[word[1]]++;
hmap[word[1]]++;
printf("%d %d ",hmap[word[0]],hmap[word[1]]);
*/
while(fscanf(fin,"%s",word)==1)
{
++hmap[word];
}
fseek(fin,0,0);
while(fscanf(fin,"%s",word)==1)
{
if(hque.size()<10)
{
hque.push(*hmap.find(word));
}
else
{
if(hmap[word]>hque.top().second)
{
hque.pop();
hque.push(make_pair(word,hmap[word]));
}

}
}
while(!hque.empty())
{
printf("%s```````````````%d\n",hque.top().first,hque.top().second);
hque.pop();
}
return 0;
}
...全文
121 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
zyang008 2011-05-12
  • 打赏
  • 举报
回复
受教了,多谢,多谢~~~
qq120848369 2011-05-11
  • 打赏
  • 举报
回复
优先队列不是按计数排序么,你怎么按字符串大小排序呢.
qq120848369 2011-05-11
  • 打赏
  • 举报
回复
啊,不是先用哈希计数再压入优先队列么.
qq120848369 2011-05-11
  • 打赏
  • 举报
回复
a
b
c
a
b
b
b
t
e
^Z
b,4
a,2
c,1
t,1
e,1
请按任意键继续. . .


在你基础上改了改。
#include <cstdio>
#include <cstdlib>
#include <queue>
#include <hash_map>
#include <string>
#include <vector>

using namespace std;
using namespace stdext;

struct CmpQueue;

typedef hash_map<string,int> HashMap;
typedef pair<string,int> QueuePair;
typedef priority_queue<QueuePair,vector<QueuePair>,CmpQueue> Heap;

struct CmpQueue
{
bool operator()(const QueuePair &left,const QueuePair &right)
{
return left.second<right.second;
}
};

int main()
{
FILE *fin=stdin;

HashMap hashMap;

char word[20];

while(fgets(word,20,fin)!=NULL)
{
word[strlen(word)-1]=0;

HashMap::iterator iter;

if( (iter=hashMap.find(word)) == hashMap.end() )
{
hashMap.insert(HashMap::value_type(word,1));
}
else
{
++iter->second;
}
}

Heap heap(hashMap.begin(),hashMap.end());

int cnt=0;

while(!heap.empty() && cnt!=10)
{
printf("%s,%d\n",heap.top().first.c_str(),heap.top().second);
heap.pop(),++cnt;
}

return 0;
}


推荐别用C的I/O了,用C++就用C++的。
qq120848369 2011-05-11
  • 打赏
  • 举报
回复
肯定不对,因为你存的都是同一个char word[]数组的地址,用string对象代替char *吧。

zyang008 2011-05-11
  • 打赏
  • 举报
回复
多谢,比较函数改过来了,
struct cmpQue{
bool operator()(pair<char*,int>p1,pair<char*,int>p2){
return stricmp(p1.first,p2.first)<0?true:false;
}
};

但是结果还是不对
我测了下,应该是
while(fscanf(fin,"%s",word)==1)
{
++hmap[word];
}
这个地方出问题了,这几句我的目的是将字符串压入hash_map里,实现计数,但是貌似不可以,不知道为什么,STL里map是可以这样用的啊···

64,700

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧