We discussed the ranges library at length in Chapter 14, Lazy Evaluation Using the Ranges Library. If you can use it, either because you use C++ 20 or because you can use it as a third-party library, the previous function becomes extremely simple and much faster:
TEST_CASE("Ranges"){
vector<Person> people = {
Person("Alex", 42),
Person("John", 21),
Person("Jane", 14),
Person("Diana", 0)
};
using namespace ranges;
string completeString = ranges::accumulate(
people |
view::transform(personToMajorMinor) |
view::transform(majorMinor),
string(),
combineWithNewline
);
string expectedString("Alex, major\nJohn, major\nJane,
minor\nInvalid person"...