bool isShorter(const string &s1,const string &s2) { return s1.size() < s2.size(); } bool GT6(const string &s) { return s.size() >= 6; } int main() { vector<string> words; string nect_word; while (cin >> next_word){ words.push_back(next_word); } sort (words.begin(), words.end()); vector<string>::iterator end_unique = unique(words.begin(), words.end()); words.erase(end_unique, words.end()); stable_sort(words.begin(),word.end(),isShorter); vector<string>::size_type wc = count_if (words.begin(), words.end(), GT6); cout << wc << " " << make_plural (wc, "word", "s") << " 6 characters or longer " << endl; return 0; }
sort函数:将容器中元素按字典排列;
unique函数:删除相邻的重复元素,然后重新排列输入范围内的元素,并且返回一个迭代器,表示无重复的值范围的结束;
谓词:做某些检测的函数,返回用于条件判断的类型,指出条件是否成立;
stable_sort函数:保留相等元素的原始相对位置,本程序里,对于相同长度的元素,将保留其字典顺序;
count_if函数:返回使谓词函数返回条件成立的元素的个数。