In today's lesson, we'll explore a comprehensive problem using the Boost.Range library in C++. Completing this lesson will help you understand how to use ranges to solve complex tasks systematically. Our goal is to calculate the average price of houses that cost more than 50,000 by filtering, transforming, and accumulating values—operations that Boost.Range simplifies tremendously.
Imagine you have a dataset filled with house prices. Our task is to find the average price of these houses, but only for those that cost more than 50,000. This problem involves multiple steps: filtering out houses costing 50,000 or less, applying a 10% discount to each remaining house price, and then calculating the average price. Let's see how we can achieve this efficiently with Boost.Range.
First, we'll introduce the necessary libraries and set up our data. Here's a code snippet for reference:
A vector is a dynamic array that can grow as needed, making it suitable for storing integer prices. The given array contains both houses costing more and less than 50,000.
Next, we need to filter out the houses costing 50,000 or less. Boost.Range provides the boost::adaptors::filtered
functionality to achieve this:
Here, boost::adaptors::filtered
uses a lambda function to filter out prices less than or equal to 50,000. The lambda function [](int price) { return price > 50000; }
returns true
for prices greater than 50,000, allowing them to pass through the filter.
Once filtered, we want to apply a 10% discount to the remaining prices. This is done using boost::adaptors::transformed
:
The transformation uses another lambda function, [](int price) { return price * 0.90; }
, which applies a 10% discount to each filtered price. We use the pipe operator to combine them
Finally, we need to sum the discounted prices and calculate the average price. For this, we use boost::accumulate
, which reduces the range to a single value starting with 0.0
:
Here, boost::accumulate
takes the transformed range and sums the elements, starting from 0.0
. We also calculate the number of houses that cost more than 50,000 using boost::distance
and finally compute the average price by dividing the total discounted price by the count of filtered houses.
Let's combine everything in the main function:
By understanding each part separately, you can see how they work together to solve a complex problem efficiently.
In this lesson, we tackled a comprehensive problem using Boost.Range. We learned to filter a range to keep only house prices greater than 50,000, transform these prices by applying a 10% discount, and then calculate the average price. This exercise showcases the power of Boost.Range in writing clean, readable, and efficient C++ code.
Now it's time to apply what you've learned. You'll move on to exercises where you'll solve similar range-based problems using Boost.Range. This hands-on practice will reinforce your understanding and help you become proficient in using Boost.Range for real-world C++ programming tasks.
