2 baidu 29472129 baidu_29472129 于 2015.07.03 11:08 提问

用VETOR进行词频统计及排序,统计做出来了,排序怎么搞,代码如下。 9C

#include #include #include using namespace std;typedef struct WORD_FREQ{ string strword; int freq;}WF;class CWordProcess {public: void OutPut(); void PushVector(string word); void Split(char pstr,char *pword); void ProcessFile(); CWordProcess(string filesrc,string filedest); virtual ~CWordProcess();private: vector m_vector; string FileDestPath; string FileSrcPath; FILE *fr; FILE *fw;};CWordProcess::CWordProcess(string filesrc,string filedest){ FileSrcPath=filesrc; FileDestPath=filedest;}CWordProcess::~CWordProcess(){ fclose(fw); fclose(fr);}void CWordProcess::ProcessFile(){ fr=fopen(FileSrcPath.c_str(),"r"); fw=fopen(FileDestPath.c_str(),"w"); char szBuf[200]={'\0'}; char szWord[200]={'\0'}; while(!feof(fr)) { fscanf(fr,"%s",szBuf); Split(szBuf,szWord); printf("%s",szWord); PushVector(szWord); } OutPut(); }void CWordProcess::Split(char *pstr, char *pword){ char *p=pstr; char *p1=strchr(p,'/'); if(p1!=NULL) { *p1='\0'; strcpy(pword,p); }}void CWordProcess::PushVector(string word){ WF wf; vector::iterator iter; for(iter=m_vector.begin();iter!=m_vector.end();iter++) { wf=(WF)*iter; if(wf.strword==word) { wf.freq++; *iter=wf; return; } } wf.strword=word; wf.freq=1; m_vector.push_back(wf);}void CWordProcess::OutPut(){ WF wf; vector::iterator iter; for(iter=m_vector.begin();iter!=m_vector.end();iter++) { wf=*iter; fprintf(fw,"%s ",wf.strword.c_str()); fprintf(fw,"%d",wf.freq); fprintf(fw,"\n"); }}#include "stdafx.h"#include "WordProcess.h"int main(int argc, char argv[]){ string strfilesrc="d:\199801.txt"; string strfiledest="d:\result.txt"; CWordProcess f(strfilesrc,strfiledest); f.ProcessFile(); printf("Hello World!\n"); return 0;}

1个回答

oyljerry
oyljerry   Ds   Rxr 2015.07.03 22:22

排序可以用std::sort
然后定义自己的比较函数

Csdn user default icon
上传中...
上传图片
插入图片