2018.10.22——10.3定制操作

本文探讨了C++中字符串处理的高级技巧,包括使用STL库进行字符串过滤及统计长度超过特定阈值的单词数量。通过实例展示了如何利用算法库函数count_if和自定义lambda表达式来高效解决问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

10.20

#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <algorithm>

using namespace std;

bool fuc(string &s)
{
	for (auto iter = s.begin(); iter != s.end() ; ++iter)
	{
		if (iter->size() > 6)											//报错  我佛了 不懂
			return true;
		else
			return false;
	}
}

int main(int argc, char *argv[])
{
	string word;
	vector<string> s;
//	ifstream in(argv[1]);
//	if (!in)
//		cerr<< "无法打开文件" <<endl;
	while (cin >> word)
		s.push_back(word);
	auto it = count_if(s.begin(), s.end(), fuc);
	cout << it << endl;
	return 0;
}

答案

#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <algorithm>
#include "make_plural.h"

using namespace std;

inline void output_words(vector<string> &words)
{
	for (auto iter = words.begin(); iter != words.end(); ++iter)
		cout << *iter << " ";
	vout << endl;
}
void biggies(vector<string> &words, vector<string>::size_type sz)
{
	output_words(words);
	auto bc = count_if(words.begin(), words.end(), [sz] (const stirng &a) {return a.size() >= sz;});
	cout << bc<< " " << make_plural(bc, "word", "s") << "of length" << sz << "or longer" << endl;
}
int main(int argc, char *argv[])
{
	string word;
	vector<string> s;
	ifstream in(argv[1]);
	if (!in)
	{
		cerr<< "无法打开文件" <<endl;
		exit(1);
	}
	vector<string> words;
	string word;
	while (in>>word)
		words.push_back(word);
	biggies(words, 6);
	return 0;
}

10.21

[i] ()mutable ->bool {if(i != 0) {--i; return false:}else return true;};

答案

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值