您的位置:首页 > 编程语言 > C语言/C++

cpp primer 验证 p346页

2010-01-09 12:05 232 查看
#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
string make_plural(size_t ctr,const string &word, const string &ending)
{
return (ctr<=1) ? word : word+ending;	//make_plural(wc, "word ", "s ")当输入中文本中
//word数大于一是在word后加s,为words为word的复数!
}
//comparison function to be used to sort by word length
bool is_shorter(const string &s1,const string &s2)
{
return s1.size()<s2.size();
}
//determine whether a length of a given word is 6 or more
bool  GT6(const string &s)
{
return s.size()>=6;
}
//main
int main()
{
vector<string> words;
//copy contents of each book into a single vector
string next_word;
while (cin>>next_word)
{
//insert next book's content at end of words
words.push_back(next_word);
}
//sort words alphabetically for finding the duplicates
sort(words.begin(),words.end());
//eliminate duplicate words
vector<string>::iterator end_unique=unique(words.begin(),words.end());
words.erase(end_unique,words.end());
stable_sort(words.begin(),words.end(),is_shorter);
vector<string>::size_type wc=count_if(words.begin(),words.end(),GT6);
cout<<wc<<"  "<<make_plural(wc,"word","s")
<<" 6 characters or longer"<<endl;
}
 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息