一个统计文件字数的小程序
来源:互联网 发布:黑帽seo 流量劫持 编辑:程序博客网 时间:2024/05/16 09:16
#ifndef WORD_COUNT_H_INCLUDED#define WORD_COUNT_H_INCLUDED#include<iostream>#include<vector>#include<string>#include<algorithm>#include<numeric>#include<fstream>#include<sstream>using namespace std;class word_count{private: vector<string> all_lines; vector<string> all_words; void delete_same_words(); void get_all_lines(char *s); void get_all_words();public: void get_allwords(int n,char *s);};bool GT6(const string hs){ return hs.size()>6;}bool my_com(const string & lhs,const string &rhs){ return lhs.size()<rhs.size();}void word_count::get_all_lines(char *s){ ifstream inf(s); string str; while(getline(inf,str)) { all_lines.push_back(str); } inf.close();}void word_count::get_all_words(){ string str; int all_lines_size=all_lines.size(); for(int i=0;i<all_lines_size;i++) { istringstream inf(all_lines[i]); while(inf>>str) all_words.push_back(str); }}void word_count::get_allwords(int n,char *s){ this->get_all_lines(s); this->get_all_words(); vector<string>::iterator it_end=unique(all_words.begin(),all_words.end()); all_words.erase(it_end,all_words.end()); stable_sort(all_words.begin(),all_words.end(),my_com); int total_num=0; /* for(vector<string>::iterator it=all_words.begin();it != all_words.end();it++) { if(it->size() > 6) { cout<<*it<<endl; } } */ for(vector<string>::iterator it=all_words.begin();it != all_words.end();it++) { it=find_if(it,all_words.end(),GT6); cout<<*it<<endl; total_num++; } cout<<"the count > 6 is :"<<total_num<<endl;}#endif // WORD_COUNT_H_INCLUDED
#include <iostream>#include "word_count.h"using namespace std;int main(){ word_count wc; char *s="text.txt"; wc.get_allwords(6,s); return 0;}