练习14.38:
#include <iostream>
#include <string>
#include <errno.h>
using namespace std;
#include <vector>
#include <fstream>
//检查某个给定的string对象的长度是否与一个阈值相等
class StringLength {
public:
//构造函数
StringLength(size_t n) :sz(n){ }
//与lambda类似的函数调用运算符
bool operator()(const string& s)const
{
return s.size() == sz;
}
private:
//阈值
size_t sz;
};
//统计单词长度为n的个数
unsigned getLengthNum(size_t n,vector<string>& v)
{
//读取文件
ifstream in("string.txt");
StringLength s(n);
string word;
unsigned num = 0;
while (in >> word)
{
if (s(word))
{
v.push_back(word);
++num;
}
}
return num;
}
//打印
void PrintVector(vector<string>& v)
{
for (auto s : v)
{
cout << s << " ";
}
cout << endl;
v.clear();
}
//测试
int main()
{
//使用vector保存长度为n的string
vector<string>v;
//单词长度为1的个数
unsigned number = getLengthNum(1,v);
cout << "长度为1的单词个数为" << number << ", 单词分别为:" << endl;
PrintVector(v);
//单词长度为2的个数
number = getLengthNum(2,v);
cout << "长度为2的单词个数为" << number << ", 单词分别为:" << endl;
PrintVector(v);
//单词长度为3的个数
number = getLengthNum(3,v);
cout << "长度为3的单词个数为" << number << ", 单词分别为:" << endl;
PrintVector(v);
//单词长度为4的个数
number = getLengthNum(4,v);
cout << "长度为4的单词个数为" << number << ", 单词分别为:" << endl;
PrintVector(v);
//单词长度为8的个数
number = getLengthNum(8,v);
cout << "长度为8的单词个数为" << number << ", 单词分别为:" << endl;
PrintVector(v);
system("pause");
return 0;
}
string.txt文件内容:
运行结果 :
练习14.39:
修改上一题的代码:
#include <iostream>
#include <string>
#include <errno.h>
using namespace std;
#include <vector>
#include <fstream>
//检查某个给定的string对象的长度是否小于阈值
class StringLength {
public:
//构造函数
StringLength(size_t n) :sz(n) { }
//与lambda类似的函数调用运算符
bool operator()(const string& s)const
{
return s.size() < sz;
}
private:
//阈值
size_t sz;
};
//统计单词长度为n的个数
unsigned getLengthNum(size_t n, vector<string>& v1, vector<string>& v2,unsigned& sum)
{
//读取文件
ifstream in("string.txt");
StringLength s(n);
string word;
unsigned num = 0;
while (in >> word)
{
++sum;
if (s(word))
{
v1.push_back(word);
++num;
}
else
{
v2.push_back(word);
}
}
return num;
}
//打印
void PrintVector(vector<string>& v)
{
for (auto s : v)
{
cout << s << " ";
}
cout << endl;
}
//测试
int main()
{
//使用vector保存长度为n的string
vector<string>v1;
vector<string>v2;
//统计单词总数
unsigned sum = 0;
//长度在1-9之间的单词的个数
unsigned number = getLengthNum(10, v1, v2, sum);
cout << "长度在1-9之间的单词个数为" << number << ", 单词分别为:" << endl;
PrintVector(v1);
//长度在10以上的单词的个数
cout << "长度在10以上的单词的个数为" << sum - number << ", 单词分别为:" << endl;
PrintVector(v2);
system("pause");
return 0;
}
结果:
练习14.40:
#include <iostream>
using namespace std;
#include <vector>
#include <string>
#include <list>
#include <fstream>
#include <algorithm>
//排序lambda类
class ShorterString {
public:
bool operator()(const string& s1, const string& s2)const
{
return s1.size() < s2.size();
}
};
//打印lambda类
class PrintString {
public:
void operator()(const string& s)const
{
cout << s << " ";
}
};
//判断阈值lambda类
class SizeComp {
public:
SizeComp(vector<string>::size_type n):sz(n){ }
bool operator()(const string& s)const
{
return s.size() >= sz;
}
private:
vector<string>::size_type sz;
};
//排序
void elimDups(vector<string>& v)
{
//排序,删除重复单词
sort(v.begin(), v.end());
auto end_unique = unique(v.begin(), v.end());
v.erase(end_unique, v.end());
}
void biggies(vector<string>& v, vector<string>::size_type sz)
{
//auto f1 = [](const string& a, const string& b) {return a.size() < b.size(); };
//auto f2 = [sz](const string& c) {return c.size() >= sz; };
elimDups(v);
//按元素长度从小到大排序,同等长度元素按字典序排序
stable_sort(v.begin(), v.end(), ShorterString());//lambda修改为类
//获取指向第一个满足size() >= sz的元素
auto fe = find_if(v.begin(), v.end(), SizeComp(sz));//lambda修改为类
auto count = v.end() - fe;
cout << "元素大小大于5的元素个数为:" << count << endl;
//打印
for_each(fe, v.end(), PrintString());//lambda修改为类
//也可以如下打印
//while (fe != v.end())
//{
// cout << *fe << " ";
// ++fe;
//}
cout << endl;
}
int main()
{
vector<string>vs;
ifstream in("story.txt");
string word;
while (in >> word)
{
vs.push_back(word);
}
cout << "文件中的单词为:-------------------------------" << endl;
//调用biggies前:
for (auto s : vs)
{
cout << s << " ";
}
cout << endl;
cout << endl;
//调用biggies后:
cout << "调用biggies后: " << endl;
biggies(vs, 5);
system("pause");
return 0;
}
结果为:
练习14.41:
lambda有自己的优点,当我们的代码需要实现一个简单的功能,且不会多次使用时,使用lambda更好,简单明了
但如果这个函数需要多次使用,那么创建为类更加方便