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实现,需要多次使用用函数对象。