注册 登录
编程论坛 C++教室

利用vector求最长最短单词

charderkk 发布于 2011-01-25 11:54, 561 次点击
  #include<iostream>
  #include<string>
  #include<vector>
  #include<algorithm>

  using namespace std;
  typedef vector<double>::size_type vec_sz;
  
  
  
  int main(){
  vector<string> passage;
  vector<double> numble;
  string x;
  cout<<"Please input one passage:"<<endl;
  
  while(cin>>x&&x!="0"){
      passage.push_back(x);
      
  }
  vec_sz count,size=passage.size();

  cout<<"The original passage is:"<<endl;
  for(count=0;count!=size;count++)
      cout<<passage[count]<<" ";
   cout<<endl;
  for(count=0;count!=size;count++)
      numble.push_back(passage[count].size());
  for(count=0;count!=size;count++)
      cout<<numble[count]<<endl;
  cout<<flush;

 sort(numble.begin(),numble.end());
  for(count=0;count!=size;count++)
      cout<<numble[count]<<endl;
  cout<<"The shortest words are:"<<endl;
  for(count=0;count!=size;++count){
      if(passage[count].size()==numble[0])
      cout<<passage[count]<<endl;
      
  }
  cout<<"The longest words are:"<<endl;
  for(count=0;count!=size;++count){
          if(passage[count].size()==numble[size-1])
      cout<<passage[count]<<endl;
      
  }
  return 0;
  
  }
这个程序只能是想对没有标点的文章求最大最小单词,想问一下如何用vector实现对带标点的一段话求最长最短单词

            





  
2 回复
#2
rjsp2011-01-25 12:40
string::find_first_of
string::find_first_not_of
#3
charderkk2011-01-26 16:34
回复 2楼 rjsp
谢了
1