forked from pezy/CppPrimer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ex10_25.cpp
50 lines (43 loc) · 1.2 KB
/
ex10_25.cpp
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
//
// ex10_25.cpp
// Exercise 10.25
//
// Created by pezy on 12/11/14.
// Copyright (c) 2014 pezy. All rights reserved.
//
// @Brief In the exercises for 10.3.2 (p.392) you wrote a version of biggies
// that uses partition.
// Rewrite that function to use check_size and bind.
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <functional>
using std::string;
using std::vector;
using namespace std::placeholders;
void elimdups(vector<string>& vs)
{
std::sort(vs.begin(), vs.end());
vs.erase(unique(vs.begin(), vs.end()), vs.end());
}
bool check_size(const string& s, string::size_type sz)
{
return s.size() >= sz;
}
void biggies(vector<string>& words, vector<string>::size_type sz)
{
elimdups(words);
auto iter = std::stable_partition(words.begin(), words.end(),
bind(check_size, _1, sz));
for_each(words.begin(), iter,
[](const string& s) { std::cout << s << " "; });
}
int main()
{
std::vector<std::string> v{"the", "quick", "red", "fox", "jumps",
"over", "the", "slow", "red", "turtle"};
biggies(v, 4);
}
// @Out
// jumps over quick slow turtle