Our first addition will be the ranges library. As you may recall, it can help us achieve elegant, simple, and declarative code. For brevity, first, we will pull in the ranges namespace:
#include <ranges> using namespace std::ranges;
We'll leave the code-defining merchants, items, and stores as-is. Let's start our modifications by using the get_featured_items_for_store function:
Items get_featured_items_for_store(const Store &store) { auto items = store.items | views::filter(&Item::featured) | views::transform([](const auto &item) { return gsl::not_null<const Item *>(&item); }); return Items(std::begin(items), std::end(items)); }
As you can see, making a range out of a container is straightforward: just pass it to a pipe operator. Instead of our hand-crafted loop to filter featured elements, we can use the views::filter expression, passing it a member pointer as the predicate...