Skip to content

Latest commit

 

History

History
53 lines (48 loc) · 1.6 KB

File metadata and controls

53 lines (48 loc) · 1.6 KB
#include <iostream>
using std::cin; using std::cout; using std::endl; using std::cerr;
#include <fstream>
using std::ifstream;
#include <string>
using std::string;
#include <vector>
using std::vector;
#include <algorithm>
using std::sort; using std::unique; using std::stable_sort;
using std::for_each; using std::stable_partition;

string make_plural(size_t ctr, const string &word = "word", const string &ending = "s"){
    return (ctr > 1) ? word + ending : word;
}

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);
    stable_sort(words.begin(), words.end(),
                [](const string &a, const string &b) -> bool{ return a.size() < b.size();});
    auto wc = stable_partition(words.begin(), words.end(),
                        [sz](const string &a) -> bool{ return a.size() >= sz;});
    auto count = wc - words.begin();
    cout << count << " " << make_plural(count) << " of length " << sz << " or longer" << endl;
    for_each(words.begin(), wc,
             [](const string &s){cout << s << " ";});
    cout << endl;
}

int main() {
    vector<string> vs;
    string pt("/home/raymain/CLionProjects/CPPLv1/test.txt");
    ifstream input(pt);
    if (input.is_open()){
        pt.clear();
        while (input >> pt)
            vs.push_back(pt);
        biggies(vs, 2);
        biggies(vs, 4);
    } else {
        cerr << "Failed to open file" << endl;
        return EXIT_FAILURE;
    }
}