返回信息流[ema1]改不出来啊!!!!!
这是一条镜像帖。来源:北邮人论坛 / cpp / #90606同步于 2016/3/18
该镜像源已超过 30 天没有更新,可能在源站已被删除。
CPP机器人发帖
[讨论][问题]来个大神帮忙改个BUG
caozheyan
2016/3/18镜像同步8 回复
订阅后,新回复会通过你的通知中心匿名送达。
8 条回复
#include <iostream>
#include <vector>
#include <fstream>
#include <string>
#include <map>
#include <queue>
#include <ctime>
using namespace std;
void topK(const int &K)
{
double t=clock();
ifstream infile("are you a pig.txt");
if (!infile)
cout<<"can not open file"<<endl;
string s="";
map<string,int>wordcount;
unsigned char temp[2];
while(true)
{
infile>>temp[0];
if(infile.eof()) break;
if (temp[0]>=0xB0)
{
s+=temp[0];
infile>>temp[1];
s+=temp[1];
}
else
{
s="";
continue;
}
wordcount[s]++;
s="";
}
cout<<"单词种类:"<<wordcount.size()<<endl;
priority_queue< pair< int,string >,vector< pair< int,string > >,allocator< pair< int,string> > > queueK;
for (map<string,int>::iterator iter=wordcount.begin(); iter!=wordcount.end(); iter++)
{
queueK.push(make_pair(iter->second,iter->first));
if(queueK.size()>K)
queueK.pop();
}
pair<int,string>tmp;
priority_queue< pair< int,string >,vector< pair< int,string > >,less< pair< int,string> > > queueKless;
while (!queueK.empty())
{
tmp=queueK.top();
queueK.pop();
queueKless.push(tmp);
}
while(!queueKless.empty())
{
tmp=queueKless.top();
queueKless.pop();
cout<<tmp.second<<"\t"<<tmp.first<<endl;
}
}
int main()
{
int k=0;
while (true)
{
cout<<"查看前K个频率最高的汉字,K=";
cin>>k;
if(k<=0)break;
topK(k);
}
return 0;
}
代码在这
VS2012 中报错, 错误 2 error C2064: 项不会计算为接受 2 个参数的函数 c:\program files\microsoft visual studio 11.0\vc\include\xutility 595
错误 3 error C2064: 项不会计算为接受 2 个参数的函数 c:\program files\microsoft visual studio 11.0\vc\include\xutility 597
priority_queue< pair< int,string >,vector< pair< int,string > > > queueK;
为何不用python。
【 在 nuanyangyang 的大作中提到: 】
: 这种写法太反人类了吧,输入输出和核心算法都在一个函数里,让人怎么看。
用shell脚本写个统计词频前10名单词的程序,只要一行哦~:
cat pig.txt | tr -cs ' ' | tr ' ' '\012' | sort | uniq -c | sort -nr | head