#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
class check_length{
public:
check_length(size_t size) : length(size) {}
bool operator()(const std::string &s) {
return s.size() == length;
}
private:
size_t length;
};
int main(int argc, char **argv) {
ifstream f(argv[1]);
if (f) {
string ret;
vector<string> v;
while (f >> ret) {
v.push_back(ret);
}
vector<int> record(10);
for (size_t i = 0; i < 10; ++i) {
record[i] = count_if(v.begin(), v.end(), check_length(i+1));
}
cout << "list the word length" << endl;
for (const auto &i:record) {
cout << record[i] << endl;
}
} else {
exit(1);
}
}
longer and shorter:
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
class check_length{
public:
check_length(size_t size) : length(size) {}
bool operator()(const std::string &s) {
return s.size() == length;
}
private:
size_t length;
};
class longer_than{
public:
longer_than(size_t size) : length(size) {}
bool operator()(const std::string &s) {
return s.size() > length;
}
private:
size_t length;
};
class shorter_than{
public:
shorter_than(size_t size) : length(size) {}
bool operator()(const std::string &s) {
return s.size() <= length;
}
private:
size_t length;
};
int main(int argc, char **argv) {
ifstream f(argv[1]);
if (f) {
string ret;
vector<string> v;
while (f >> ret) {
v.push_back(ret);
}
size_t numbers = count_if(v.begin(), v.end(), longer_than(10));
cout << "longer than 10 words " << numbers << endl;
} else {
exit(1);
}
}