C++ Primer(第五版) 14.8.1节练习

14.38    

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

using namespace std;

class SizeComp {
public:
	SizeComp(size_t n): sz(n) { }
	bool operator()(const string &s) const { return s.size() == sz; }
private:
	size_t sz;
};

int main(int argc, char *argv[])
{
	vector<string> words;
	string word;
	
	ifstream in(argv[1]);
	string line;
	
	while (getline(in, line)) {
		istringstream sin(line);
		while (sin >> word) {
			if (ispunct(word[0]))
				word.erase(0, 1);
			if (ispunct(word[word.size()-1]))
				word.pop_back();
			if (ispunct(word[word.size()-1]))
				word.pop_back();
			words.push_back(word);
			cout << word << " ";
		}
		cout << endl;
	}

	cout << "text gain" << endl;
	
	
	for (size_t i = 1; i < 11; ++i) {
		int cnt = 0;
		auto wc = words.begin();
		
		while (wc != words.end()) {
			wc = find_if(wc, words.end(), SizeComp(i)); 
			if (wc != words.end()) {
				cout << *wc << " ";
				wc++;
				++cnt;
			}
		}
		cout << endl;
		cout << "length " <<  i << " has " << cnt << endl;
	}
	
	return 0;
}

14.39    

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

using namespace std;

class SizeBetween {
public:
	SizeBetween(size_t min, size_t max): min_sz(min), max_sz(max) { }
	bool operator()(const string &s) const { return s.size() >= min_sz && s.size() <= max_sz; }
private:
	size_t min_sz, max_sz;
};

class SizeLonger {
public:
	SizeLonger(size_t len): sz(len) { }
	bool operator()(const string &s) const { return s.size() >= sz; }
private:
	size_t sz;
};


int main(int argc, char *argv[])
{
	vector<string> words;
	string word;
	
	ifstream in(argv[1]);
	string line;
	
	while (getline(in, line)) {
		istringstream sin(line);
		while (sin >> word) {
			if (ispunct(word[0]))
				word.erase(0, 1);
			if (ispunct(word[word.size()-1]))
				word.pop_back();
			if (ispunct(word[word.size()-1]))
				word.pop_back();
			words.push_back(word);
			cout << word << " ";
		}
		cout << endl;
	}

	cout << "text gain" << endl;
	
	int cnt = 0;
	auto wc = words.begin();
		
	while (wc != words.end()) {
		wc = find_if(wc, words.end(), SizeBetween(1, 9)); 
		if (wc != words.end()) {
			cout << *wc << " ";
			wc++;
			++cnt;
		}
	}
	cout << endl;
	cout << "length 1~9 " << "has " << cnt << endl;

	cnt = 0;
	wc = words.begin();
	
	while (wc != words.end()) {
		wc = find_if(wc, words.end(), SizeLonger(10)); 
		if (wc != words.end()) {
			cout << *wc << " ";
			wc++;
			++cnt;
		}
	}
	cout << endl;
	cout << "length longer than 10" <<  " has " << cnt << endl;
	
	return 0;
}

14.40    

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

using namespace std;

class IsShorter {
public:
	bool operator()(const string &s1, const string &s2) { return s1.size() < s2.size(); }
};

class LongerThan {
public:
	LongerThan(size_t sz): sz(sz) { } 
	bool operator()(const string &s) { return s.size() > sz; }
private:
	size_t sz;
};

class PrintStr {
public:
	void operator()(const string &s) { cout << s << " "; }
};

void elimDups(vector<string> &words)
{
	sort(words.begin(), words.end());
	auto end_unique = unique(words.begin(), words.end());
	words.erase(end_unique, words.end());
}

void biggies(vector<string> &words, vector<string>::size_type sz)
{
	elimDups(words);
	IsShorter is;
	stable_sort(words.begin(), words.end(), is);
	
	LongerThan longer(sz);
	auto wc = find_if(words.begin(), words.end(), longer);
	auto cnt = words.end() - wc;
	
	PrintStr ps;
	for_each(wc, words.end(), ps);
			 
	cout << endl;
}

int main(int argc, char *argv[])
{
	ifstream in(argv[1]);
	string line, word;
	vector<string> words;
	
	while (getline(in, line)) {
		istringstream text(line);
		while (text >> word)
			words.push_back(word);
	}
	
	biggies(words, 4);
	
	return 0;
}

14.41    lambda是通过匿名的函数对象来实现的,可看作函数对象的简化使用方式;当函数只用一次,可以用lambda实现,需要多次使用用函数对象。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值