forked from Mooophy/Cpp-Primer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex10_13.cpp
45 lines (36 loc) · 1.24 KB
/
ex10_13.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
//! @Alan
//!
//! Exercise 10.13:
//! The library defines an algorithm named partition that takes a predicate
//! and partitions the container so that values for which the predicate is
//! true appear in the first part and those for which the predicate is false
//! appear in the second part. The algorithm returns an iterator just past
//! the last element for which the predicate returned true. Write a function
//! that takes a string and returns a bool indicating whether the string has
//! five characters or more. Use that function to partition words. Print the
//! elements that have five or more characters.
//!
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
//! Predicate
inline
bool isLongerThan5(const std::string &s)
{
return s.size() >= 5;
}
void partition_words(std::vector<std::string> &v)
{
auto iter_longerLast = std::partition(v.begin(), v.end(), isLongerThan5);
//! @note the range to be printed not whole of the v, so can't use for range.
for(auto it = v.begin(); it != iter_longerLast; ++it)
std::cout << *it << " ";
std::cout << std::endl;
}
int main()
{
std::vector<std::string> v{"a","as","aasss","aaaaassaa","aaaaaabba","aaa"};
partition_words(v);
return 0;
}