Chapter 21 - Exercises

Exercise 1 - Basics

  1. What are unary and binary functions?

  2. What are predicates?

Exercise 2 - Function Object Types

For each of the following functions, create a variable to store the function as a function object:

void first() {}
int second() { return 1; }
int third(int x) { return x; }
int fourth(int x, int y) { return x * y; }

Exercise 3 - Unary Functions

Part 1

Create a unary function that accumulates the values that were used to invoked it.

auto intAccumulator = Accumulator<int>();
intAccumulator(5);
intAccumulator(10);
intAccumulator(13);
std::cout << intAccumulator.result() << std::endl;  // Prints 28 

Use this function with the std::for_each function to accumulate all values in a vector of integers.

auto accumulator = std::for_each(numbers.begin(), numbers.end(), Accumulator<int>());
std::cout << accumulator.sum() << std::endl;

Verify that the answer is correct.

The std::for_each function returns the function object that was originally passed as the third argument.

Part 2

Create a unary predicate that determines whether a number is a multiple of another.

auto isMultiple = IsMultiple(5);
if (isMultiple(10)) {
    std::cout << "10 is a multiple of 5" << std::endl;
}

Use this unary predicate with the std::find_if function to find the first element in a vector of integers that is a multiple of a value that the user has entered.

Part 3

Create a unary predicate that determines whether a string begins with another string.

auto beginsWithPre = BeginsWith("pre");
if (beginsWithPre("precondition")) {
    std::cout << "precondition begins with pre" << std::endl;
}

Use this unary predicate with the std::find_if function to find the first element in a vector of strings that begins with a string chosen by the user.

Exercise 4 - Binary Functions

Part 1

Create a binary function that simply adds two numerical values together.

auto multiply = Multiply<double>();
std::cout << multiply(3.0, 4.0) << std::endl;  // Returns 12.0 

Use this binary function with the std::transform function that performs a pairwise multiplication between two vectors, and stores the results in a third result vector.

Part 2

Create a Person struct that has a string member variable name, and an integer member variable age.

Create a binary function called ConvertToPerson that accepts a string parameter name, and an integer parameter age, and returns a Person with the given name and age.

Create a vector of strings called names with three random names of your choice.

Create a vector of integers called ages with three random numbers of your choice.

Now, use the std::transform function to perform a pairwise enumeration over the names and ages vectors, to create a vector of Persons

std::vector<Person> people;
people.resize(3);

std::transform(
    names.begin(),
    names.end(),
    ages.begin(),
    people.begin(),
    ConvertToPerson()
)

Part 3

Create a binary predicate that helps sort a vector of integers in descending order.

std::vector<int> numbers = {5, 10, 2, 4, 1};
std::sort(numbers.begin(), numbers.end(), DescendingSort<int>());

Last updated

Was this helpful?