Menu
Coddy logo textTech

Functors & Lambda Expressions

Part of the Object Oriented Programming section of Coddy's C++ journey — lesson 74 of 104.

Many STL algorithms accept a callable object that customizes their behavior. You've already seen lambdas used with std::transform. Let's explore both functors and lambda expressions - two ways to create callable objects in C++.

A functor (function object) is a class that overloads the operator(), allowing instances to be called like functions:

#include <algorithm>
#include <vector>
#include <iostream>

struct MultiplyBy {
    int factor;
    MultiplyBy(int f) : factor(f) {}
    
    int operator()(int x) const {
        return x * factor;
    }
};

int main() {
    std::vector<int> nums = {1, 2, 3, 4};
    std::vector<int> result(nums.size());
    
    std::transform(nums.begin(), nums.end(), result.begin(), MultiplyBy(3));
    // result: {3, 6, 9, 12}
}

Functors can maintain state between calls, which regular functions cannot. However, defining a class for simple operations is verbose. Lambda expressions provide a concise alternative:

int factor = 3;
std::transform(nums.begin(), nums.end(), result.begin(),
               [factor](int x) { return x * factor; });

The lambda syntax is [capture](parameters) { body }. The capture clause specifies which outside variables the lambda can access. Use [=] to capture all by value, [&] to capture all by reference, or list specific variables like [factor] or [&factor].

Lambdas are particularly useful for one-off operations with algorithms like sorting with custom criteria:

std::vector<int> nums = {5, -2, 8, -1};
std::sort(nums.begin(), nums.end(), 
          [](int a, int b) { return std::abs(a) < std::abs(b); });
// Sorted by absolute value: {-1, -2, 5, 8}
challenge icon

Challenge

Easy

Let's build a price calculator that demonstrates both functors and lambda expressions for applying different discount strategies to product prices.

You'll organize your code across two files:

  • Discounts.h: Define your discount functors and utility functions here.

    Create a functor called PercentageDiscount that stores a discount percentage (as an integer). Its operator() should take a double price and return the discounted price. For example, a 20% discount on $100 should return $80.

    Create another functor called FixedDiscount that stores a fixed amount to subtract (as a double). Its operator() should take a price and return the price minus the fixed amount (but never below 0).

    Create a function called printPrices that takes a const std::vector<double>& and prints all prices separated by spaces, followed by a newline. Format each price with two decimal places.

  • main.cpp: Read five inputs (each on a separate line):
    1. First product price (double)
    2. Second product price (double)
    3. Third product price (double)
    4. Percentage discount to apply (integer, e.g., 20 for 20%)
    5. Fixed discount amount (double)

    Create a vector with the three prices and demonstrate both approaches:

    1. Print Original prices: followed by the prices
    2. Use std::transform with your PercentageDiscount functor to create a new vector of discounted prices. Print After percentage discount: followed by the results
    3. Use std::transform with your FixedDiscount functor on the original prices to create another vector. Print After fixed discount: followed by the results
    4. Use std::transform with a lambda expression that doubles each original price. Print Premium prices (doubled): followed by the results
    5. Use std::sort with a lambda to sort the original prices in descending order. Print Sorted (high to low): followed by the sorted prices

For example, with inputs 100.00, 50.00, 75.00, 20, and 15.00:

Original prices: 100.00 50.00 75.00 
After percentage discount: 80.00 40.00 60.00 
After fixed discount: 85.00 35.00 60.00 
Premium prices (doubled): 200.00 100.00 150.00 
Sorted (high to low): 100.00 75.00 50.00 

This challenge lets you compare functors (which maintain state like the discount amount) with lambdas (which capture variables for quick, inline operations). Both approaches work seamlessly with STL algorithms like std::transform and std::sort.

Cheat sheet

STL algorithms accept callable objects to customize their behavior. Two common approaches are functors and lambda expressions.

A functor is a class that overloads operator(), allowing instances to be called like functions:

struct MultiplyBy {
    int factor;
    MultiplyBy(int f) : factor(f) {}
    
    int operator()(int x) const {
        return x * factor;
    }
};

std::transform(nums.begin(), nums.end(), result.begin(), MultiplyBy(3));

Functors can maintain state between calls, but require verbose class definitions.

Lambda expressions provide a concise alternative with syntax [capture](parameters) { body }:

int factor = 3;
std::transform(nums.begin(), nums.end(), result.begin(),
               [factor](int x) { return x * factor; });

The capture clause specifies which outside variables the lambda can access:

  • [=] - capture all by value
  • [&] - capture all by reference
  • [factor] - capture specific variable by value
  • [&factor] - capture specific variable by reference

Lambdas work well with algorithms like std::sort for custom sorting criteria:

std::sort(nums.begin(), nums.end(), 
          [](int a, int b) { return std::abs(a) < std::abs(b); });

Try it yourself

#include <iostream>
#include <vector>
#include <algorithm>
#include "Discounts.h"

int main() {
    // Read inputs
    double price1, price2, price3;
    int percentageDiscount;
    double fixedDiscount;
    
    std::cin >> price1;
    std::cin >> price2;
    std::cin >> price3;
    std::cin >> percentageDiscount;
    std::cin >> fixedDiscount;
    
    // Create a vector with the three prices
    std::vector<double> prices = {price1, price2, price3};
    
    // TODO: Print "Original prices:" followed by the prices using printPrices
    
    // TODO: Use std::transform with PercentageDiscount functor
    // Create a new vector for the results
    // Print "After percentage discount:" followed by the results
    
    // TODO: Use std::transform with FixedDiscount functor on original prices
    // Create a new vector for the results
    // Print "After fixed discount:" followed by the results
    
    // TODO: Use std::transform with a lambda that doubles each original price
    // Create a new vector for the results
    // Print "Premium prices (doubled):" followed by the results
    
    // TODO: Use std::sort with a lambda to sort original prices in descending order
    // Print "Sorted (high to low):" followed by the sorted prices
    
    return 0;
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Object Oriented Programming