c++统计单词的个数
(2012-07-09 11:34:07)
标签:
it |
分类: c primer |
#include <iostream>
#include<vector>
#include<string>
#include<list>
#include<map>
#include<sstream>
#include<fstream>
#include<stdarg.h>
using namespace std;
int main()
{
string s("I was: you, not");
string com(": ,");
string::size_type pos=0,pos2=0;
int cc=0;
//找到前面几个单词,最后一个单词单独处理
cout<<"the word is listed as follows:"<<endl;
while((pos=s.find_first_not_of(com,pos))!=string::npos&&(pos2=s.find_first_of(com,pos2))!=string::npos)
if(pos2>pos)
{
++cc;
cout<<s.substr(pos,pos2-pos)<<endl;
++pos2;pos=pos2;
}
else
{
pos2=pos;
continue;
}
pos=s.find_last_of(com);
if((pos+1)!=string::npos)
cout<<s.substr(pos+1)<<endl;
cout<<"the total number:";
cout<<cc+1<<endl;
return
0;
}
#include<vector>
#include<string>
#include<list>
#include<map>
#include<sstream>
#include<fstream>
#include<stdarg.h>
using namespace std;
int main()
{
string s("I was: you, not");
string com(": ,");
string::size_type pos=0,pos2=0;
int cc=0;
//找到前面几个单词,最后一个单词单独处理
cout<<"the word is listed as follows:"<<endl;
while((pos=s.find_first_not_of(com,pos))!=string::npos&&(pos2=s.find_first_of(com,pos2))!=string::npos)
if(pos2>pos)
{
cout<<s.substr(pos,pos2-pos)<<endl;
}
else
{
}
pos=s.find_last_of(com);
if((pos+1)!=string::npos)
cout<<s.substr(pos+1)<<endl;
cout<<"the total number:";
cout<<cc+1<<endl;
}
后一篇:修改map容器比较方式