-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathex1022.cpp
More file actions
45 lines (34 loc) · 1.12 KB
/
ex1022.cpp
File metadata and controls
45 lines (34 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <functional>
void remove_duplicates(std::vector<std::string> &s_vec)
{
std::sort(s_vec.begin(), s_vec.end());
auto unique_end = std::unique(s_vec.begin(), s_vec.end());
s_vec.erase(unique_end, s_vec.end());
}
bool is_shorter(const std::string &a, const std::string &b)
{
return a.size() < b.size();
}
bool has_less_than(const std::string &str, const int &sz)
{
return str.size() < sz;
}
void biggies(std::vector<std::string> words, std::vector<std::string>::size_type sz)
{
remove_duplicates(words);
std::stable_sort(words.begin(), words.end(), std::bind(is_shorter, std::placeholders::_1, std::placeholders::_2));
auto num_of_words = std::count_if(words.begin(), words.end(),std::bind(has_less_than, std::placeholders::_1, sz));
std::cout << "There are " << num_of_words << " word(s) of size less than " << sz << std::endl;
}
int main()
{
std::vector<std::string> words = {
"apple", "zebra", "cat", "banana", "dog",
"apple", "fig", "banana", "grape", "house"
};
biggies(words, 6);
}