forked from Mooophy/Cpp-Primer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex10_13.cpp
34 lines (29 loc) · 1.04 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
// @Yue Wang
//
// 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>
bool predicate(const std::string &s)
{
return s.size() >= 5;
}
int main()
{
auto v = std::vector<std::string>{ "a", "as", "aasss", "aaaaassaa", "aaaaaabba", "aaa" };
auto pivot = std::partition(v.begin(), v.end(), predicate);
for (auto it = v.cbegin(); it != pivot; ++it)
std::cout << *it << " ";
std::cout << std::endl;
return 0;
}