1. void selectsentence()  
  2. {  
  3.     string line1("We were her pride of 10 she named us:");  
  4.     string line2("Benjamin,Phoenix,the Prodigal");  
  5.     string line3("and perspicacious pacific Suzanne");  
  6.     string sentence=line1+" "+line2+" "+line3;  
  7.     string seperate(" \n\v\r\a\? : ,\t\b\f");  
  8.     vector<string>longword,shortword;  
  9.     string word,Lword,Sword;  
  10.     int count=0;  
  11.     string::size_type startpos=0,endpos=0,slen;  
  12.     while((startpos=sentence.find_first_not_of(seperate,endpos))!=string::npos)  
  13.     {  
  14.        ++count;  
  15.        endpos=sentence.find_first_of(seperate,startpos);  
  16.       // word=sentence.substr(startpos,endpos);  
  17.        cout<<endpos<<endl;  
  18.        if(endpos==string::npos)  
  19.        {  
  20.            slen=sentence.size()-startpos;  
  21.        }  
  22.        else  
  23.        {  
  24.            slen=endpos-startpos;  
  25.  
  26.        }  
  27.        word.assign(sentence.begin()+startpos,sentence.begin()+startpos+slen);  
  28.       // startpos=sentence.find_first_not_of(seperate,endpos);  
  29.        cout<<word<<endl;  
  30.        if(count==1)  
  31.        {  
  32.            Lword=Sword=word;  
  33.            longword.push_back(Lword);  
  34.            shortword.push_back(Sword);  
  35.        }  
  36.        else  
  37.        {   if(word.size()>Lword.size())  
  38.             {  
  39.                 longword.clear();  
  40.                    Lword=word;  
  41.                    longword.push_back(Lword);  
  42.  
  43.             }  
  44.           else   if(word.size()==Lword.size())  
  45.                {      
  46.                    Lword=word;  
  47.                    longword.push_back(Lword);  
  48.  
  49.                }  
  50.          else   if(word.size()<Sword.size())  
  51.                {     
  52.                    shortword.clear();  
  53.                    Sword=word;  
  54.                    shortword.push_back(Sword);  
  55.                      
  56.                }  
  57.          else if(word.size()==Sword.size())  
  58.                {  
  59.                    Sword=word;  
  60.                    shortword.push_back(Sword);  
  61.                }  
  62.  
  63.        }  
  64.          
  65.  
  66.  
  67.     }  
  68.     cout<<"单词的个数为"<<count<<endl;  
  69.     vector<string>::iterator longw=longword.begin(),shortw=shortword.begin();  
  70.     cout<<"最长的单词为";  
  71.     while(longw!=longword.end())  
  72.         cout<<*longw++<<endl;  
  73.     cout<<"最短的单词为";  
  74.     while(shortw!=shortword.end())  
  75.         cout<<*shortw++<<endl;  
  76.  
  77.  

endpos的值在到最后的时候返回的是失败的值,所以必须考虑进去,,,
 

里边有一点需要主要的 
   if(endpos==string::npos)
   {
   slen=sentence.size()-startpos;
   }
   else
   {
   slen=endpos-startpos;

   }