65,211
社区成员
发帖
与我相关
我的任务
分享#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
string line1="We were her prede of 10 she named us: ";
string line2="Benjamin, Phoenix, the Prodegal";
string line3="and perspicacious pacific Suzanne";
string sentence=line1+' '+line2+' '+line3;
string separators=" \t:,\v\r\n\f";
string word;
string::size_type starpos=0,endpos=0,maxlen,minlen,count=0,wordlen;
vector<string> maxvec,minvec;
while((starpos=sentence.find_first_not_of(separators,endpos))!=string::npos)
{
++count;
endpos=sentence.find_first_of(separators,starpos);
if(endpos==string::npos)
wordlen=sentence.size()-starpos;
else wordlen=endpos-starpos;
word.assign(sentence.begin()+starpos,sentence.end()+starpos+wordlen);
//starpos=sentence.find_first_not_of(separators,endpos);
if(count==1)
{maxlen=wordlen;
minlen=wordlen;
maxvec.push_back(word);
minvec.push_back(word);
}
else {
if(wordlen>maxlen)
{
maxvec.clear();
maxlen=wordlen;
maxvec.push_back(word);
}
else if(wordlen==maxlen)
maxvec.push_back(word);
if(wordlen<minlen)
{minvec.clear();
minlen=wordlen;
minvec.push_back(word);
}
else
minvec.push_back(word);
}
}
cout<<"word amount: "<<count<<endl;
vector<string>::iterator iter;
cout<<"longest word(s):"<<endl;
iter=maxvec.begin();
while(iter!=maxvec.end())
cout<<*iter++<<endl;
cout<<"shotest word(s):"<<endl;
iter=minvec.begin();
while(iter!=minvec.end())
cout<<*iter++<<endl;
return 0;
}
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
string line1="We were her prede of 10 she named us:";
string line2="Benjamin,Phoenix,the Prodegal";
string line3="and perspicacious pacific Suzanne";
string sentence=line1+' '+line2+' '+line3;
string separators=" \t:,\v\r\n\f";
string word;
string::size_type starpos=0,endpos=0,maxlen,minlen,count=0,wordlen;
vector<string> maxvec,minvec;
while((starpos=sentence.find_first_not_of(separators,endpos))!=string::npos)
{
++count;
endpos=sentence.find_first_of(separators,starpos);
if(endpos==string::npos)
wordlen=sentence.size()-starpos;
else
wordlen=endpos-starpos;
word.assign(sentence.begin()+starpos,sentence.begin()+starpos+wordlen);//Here----sentence.begin()
//starpos=sentence.find_first_not_of(separators,endpos);
if(count==1)
{
maxlen=wordlen;
minlen=wordlen;
maxvec.push_back(word);
minvec.push_back(word);
}
else
{
if(wordlen>maxlen)
{
maxvec.clear();
maxlen=wordlen;
maxvec.push_back(word);
}
else if(wordlen==maxlen)
maxvec.push_back(word);
if(wordlen<minlen)
{
minvec.clear();
minlen=wordlen;
minvec.push_back(word);
}
else if(wordlen==minlen)//--------Here
minvec.push_back(word);
}
}
cout<<"word amount: "<<count<<endl;
vector<string>::iterator iter;
cout<<"longest word(s):"<<endl;
iter=maxvec.begin();
while(iter!=maxvec.end())
cout<<*iter++<<endl;
cout<<"shotest word(s):"<<endl;
iter=minvec.begin();
while(iter!=minvec.end())
cout<<*iter++<<endl;
return 0;
}
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
string line1="We were her prede of 10 she named us:";
string line2="Benjamin,Phoenix,the Prodegal";
string line3="and perspicacious pacific Suzanne";
string sentence=line1+' '+line2+' '+line3;
string separators=" \t:,\v\r\n\f";
string word;
string::size_type starpos=0,endpos=0,maxlen,minlen,count=0,wordlen;
vector<string> maxvec,minvec;
while((starpos=sentence.find_first_not_of(separators,endpos))!=string::npos)
{
++count;
endpos=sentence.find_first_of(separators,starpos);
if(endpos==string::npos)
wordlen=sentence.size()-starpos;
else
wordlen=endpos-starpos;
word.assign(sentence.begin()+starpos,sentence.begin()+starpos+wordlen);//Here----sentence.begin()
//starpos=sentence.find_first_not_of(separators,endpos);
if(count==1)
{
maxlen=wordlen;
minlen=wordlen;
maxvec.push_back(word);
minvec.push_back(word);
}
else
{
if(wordlen>maxlen)
{
maxvec.clear();
maxlen=wordlen;
maxvec.push_back(word);
cout<<word<<endl;
system("pause");
}
else if(wordlen==maxlen)
maxvec.push_back(word);
if(wordlen<minlen)
{
minvec.clear();
minlen=wordlen;
minvec.push_back(word);
}
else if(wordlen==minlen)//Here
minvec.push_back(word);
}
}
cout<<"word amount: "<<count<<endl;
system("pause");
vector<string>::iterator iter;
cout<<"longest word(s):"<<endl;
iter=maxvec.begin();
while(iter!=maxvec.end())
cout<<*iter++<<endl;
system("pause");
cout<<"shotest word(s):"<<endl;
iter=minvec.begin();
while(iter!=minvec.end())
cout<<*iter++<<endl;
return 0;
}