C++primer 练习10.16

时间:2023-03-08 16:11:44
// 10.3.2.cpp : 定义控制台应用程序的入口点。
// #include "stdafx.h"
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std; string & make_plural(int cnt, string &str)
{
str=cnt > ? (str + "s") : str;
return str;
} void biggies(vector<string> &words, vector<string>::size_type sz)
{
sort(words.begin(), words.end());
//按照默认形式对容器中的元素排序 auto uniqIte = unique(words.begin(), words.end());
//将重复的元素放到迭代器范围的后面,并且返回第一个重复元素的迭代器 words.erase(uniqIte,words.end());
//删除重复的元素
//stable_sort(words.begin(), words.end(), [](const string &s1, const string &s2) {return s1.size() < s2.size();});
//这个是书上的写法 auto ite=partition(words.begin(), words.end(), [sz](const string &s) {return s.size() >= sz;});
//返回第一个迭代器范围内不满足lambda表达式的元素,并且将满足表达式的元素放在范围前面,不满足的放在后面
unsigned int cnt = ;
for (auto beg = words.begin();beg != ite;++beg, ++cnt);
//得到长度不小于sz的字符串的个数 cout << cnt << " " << make_plural(cnt, string("word")) << " of length " <<
sz << " or longer" << endl; for_each(words.begin(), ite, [](const string &s) {cout << s << " ";});
//输出迭代器范围内的元素(即长度不小于sz的元素)
cout << endl;
} int main()
{
vector<string> strVec = { "lidanda","hahaha","liyou","wumei","ni","sansan" };
biggies(strVec, );
return ;
}