先拿到文件中的单词,利用split函数分隔,原理是string的find_of_first函数,第二个参数很好用,可以根据分号逗号等等任意分隔。将分隔的一个一个单词存储到vector中。 然后遍历vector,存储在multimpa中,使用键值对方式,健是string,值是int,使用find函数,如果map中有则值加一,没有则放入。multimap中的会按照第一个元素排序输出, 即string的字典序输出。如何按照int从大到小输出?将map的值放到vector中,使用自定义排序写一个cmp函数,即可、
#include <algorithm>
#include <iostream>
#include <cstring>
#include<fstream>
#include <vector>
#include <map>
using namespace std;
//存储被切割后的string的容器
vector<string> res;
//
vector<pair<string,int>> vec;
//匹配逗号空格等等!正则表达式
string Delema=" ,.\"";
fstream datafile;
string filename = "a.txt",wordline;
typedef multimap<string,int> map_std;
map_std::iterator it;
map_std mp;
//分割字符函数,不用细看
void split(string& s,vector<string>&res,string& delema1){
string::size_type start=s.find_first_not_of(delema1,0);//找到第一个不为逗号的下标
string::size_type pose=s.find_first_of(delema1,start);//找到第一个逗号的下标
while(string::npos!=start||string::npos!=pose){//当即没有逗号也没有字符的时候结束
res.push_back(s.substr(start,pose-start));
start=s.find_first_not_of(delema1,pose);//更新start 从pose开始
pose=s.find_first_of(delema1,start);//更新pos,从start开始
}
}
void get_file(){
cout<<"请输入要处理的英文附件!"<<endl;
cin>>filename;
datafile.open(filename.c_str(),fstream::in);
if(datafile.is_open() == false){
cout<<"文件打开失败!"<<endl;
}else{
while(getline(datafile,wordline)){
split(wordline,res,Delema);
}
}
// for(auto& i:res)
// cout<<i<<endl;
//给res的单词放在map中
vector<string>::iterator first = res.begin();
vector<string>::iterator last = res.end();
int num =1;
string word="a";
while(first!=last){
word = *first;
//现在mp中找找有没有word
// cout<<*first++<<endl;
first++;
it = mp.find(word);
if(it == mp.end()){
//mp中没有这个单词
mp.insert(make_pair(word,num));
}else{
//mp中有这个单词!
it->second++;
}
}
}
bool cmp(pair<string,int> a,pair<string,int> b){
return a.second > b.second;
}
void task1(){
for(map_std::iterator p=mp.begin();p!=mp.end();p++){
vec.push_back(make_pair(p->first,p->second));
}
sort(vec.begin(), vec.end(), cmp);
for (auto i = 0; i < vec.size(); ++i)
cout << vec[i].first << " " << vec[i].second << endl;
}
void task2(){
map_std::iterator p = mp.begin();
for(p = mp.begin();p!=mp.end();p++){
cout<<p->first<<" "<<p->second<<endl;
}
}
void task3(){
// for(auto& i:res)
// cout<<i<<endl;
vector<string>::iterator first = res.begin();
vector<string>::iterator last = res.end();
while(first !=last)
cout<<*--last<<endl;
}
void print(){
printf("\t1.按单词个数输出\n");
printf("\t2.按单词顺序输出\n");
printf("\t3.逆序输出\n");
}
void menu(){
system("pause");
system("cls");
system("color 4E");//黑框界面颜色
printf("************欢迎进入系统***********\n");
}
int main() {
get_file();//先拿到txt文件中的单词
// int num;
// string word;
//
// cin>>word;
// num=1;
// mp.insert(make_pair(word,num));
// cin>>word;
// mp.insert(make_pair(word,num));
// cin>>word;
// mp.insert(make_pair(word,num));
//
// it = mp.find("aa");
// if(it == mp.end()){
// cout<<"0000000000"<<endl;
// }else{
// cout<<"zhaodao le !!!\n";
// cout<<it->first<<" "<<it->second<<endl;
// it->second++;
// cout<<it->first<<" "<<it->second<<endl;
// }
//
// map_std::iterator p = mp.begin();
//
// for(p = mp.begin();p!=mp.end();p++){
// cout<<p->first<<" "<<p->second<<endl;
// }
//
while(1){
menu();
print();
int choice ;
scanf("%d",&choice);
if(choice == 1){
task1();
}else if(choice ==2){
task2();
}else if(choice ==3){
task3();
}else if(choice ==4){
break;
}else{
cout<<"输入有误,请重新输入!"<<endl;
}
}
return 0;
}
废江博客 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 转载请注明原文链接:c++统计英文文章的单词数,花式输出!