concept people in category c++

This is an excerpt from Manning's book Functional Programming in C++.
Functional programmers claim it’s easier to understand programs written in the functional style. This is subjective, and people who are used to writing and reading imperative code may disagree. Objectively, it can be said that programs written in the functional style tend to be shorter and more concise. This was apparent in the earlier example: it started with 20 lines of code and ended up with a single line for
count_lines_in_filesand about 5 lines forcount_lines, which mainly consisted of boilerplate imposed by C++ and STL. Achieving this was possible using higher-level abstractions provided by the FP parts of STL.
Let’s illustrate this with an example. Imagine that you have a group of people, and need to write the names of all the females in the group (see figure 2.1).
Figure 2.1 Filtering a collection of people based on the
is_femalepredicate. It should return a new collection containing only females.![]()
The next step is to get the names of the people in the filtered collection. You can use
std::transformto do this. You need to pass it the input collection as a pair of iterators, the transformation function, and where the results should be stored. In this case, you know that the number of names is the same as the number of females, so you can immediately create thenamesvector to be the size required to store the result and usenames.begin()as the destination iterator instead of relying onstd::back_inserter. This way, you’ll eliminate the potential memory reallocations needed for vector resizing.Listing 2.9 Getting the names
std::vector<std::string> names(females.size()); std::transform(females.cbegin(), females.cend(), #1 names.begin(), #2 name); #3 #1 What you’re transforming #2 Where to store the results #3 Transformation functionYou’ve split a larger problem into two smaller ones. This way, instead of creating one highly specialized function that will be of limited use, you create two separate, much more broadly applicable functions: one that filters a collection of people based on a predicate, and one that retrieves the names of people in a collection. The code reusability is thus much higher.