-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathex1025.cpp
More file actions
52 lines (40 loc) · 1.31 KB
/
ex1025.cpp
File metadata and controls
52 lines (40 loc) · 1.31 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
46
47
48
49
50
51
52
#include <iostream>
#include <vector>
#include <string>
#include <functional>
#include <algorithm>
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 check_size(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(),
[](const std::string &a, const std::string &b)
{
return a.size() < b.size();
}
);
// returns an iterator to the first object for which the condition holds
auto partition_end = std::partition(words.begin(), words.end(), std::bind(check_size, std::placeholders::_1, sz));
std::vector<std::string>::difference_type num_of_words = partition_end - words.begin();
std::cout << "There are " << num_of_words << " word(s) of size " << sz << " or bigger!" << std::endl;
}
int main()
{
std::vector<std::string> words = {
"apple", "zebra", "cat", "banana", "dog",
"apple", "fig", "banana", "grape", "house"
};
biggies(words, 2);
biggies(words, 3);
biggies(words, 4);
biggies(words, 5);
}