Project 6: Algorithm and Lambda Workout

A single program that solves a series of data manipulation puzzles. Given a std::vector of Person objects (with name, age, city), you will complete tasks like sorting by different criteria, filtering, transforming, and counting elements, with each task being solved by a single, appropriate STL algorithm and a lambda.

Quick Reference

Attribute Value
Primary Language C++
Alternative Languages N/A
Difficulty Level 1: Beginner
Time Estimate Weekend
Knowledge Area Algorithms / Lambdas / Functional Programming
Tooling N/A
Prerequisites Basic C++ syntax.

What You Will Build

A single program that solves a series of data manipulation puzzles. Given a std::vector of Person objects (with name, age, city), you will complete tasks like sorting by different criteria, filtering, transforming, and counting elements, with each task being solved by a single, appropriate STL algorithm and a lambda.

Why It Matters

This project builds core skills that appear repeatedly in real-world systems and tooling.

Core Challenges

  • Sorting with a custom predicate → maps to passing a lambda to std::sort
  • Finding elements that match a condition → maps to using std::find_if
  • Counting elements that match a condition → maps to using std::count_if
  • Transforming data from one form to another → maps to using std::transform
  • Removing elements from a container → maps to the erase-remove idiom with std::remove_if

Key Concepts

  • Lambda Expressions: The core of modern C++ functional style.
  • The Erase-Remove Idiom: The correct way to remove elements from a vector.
  • Common STL Algorithms: find_if, count_if, transform, for_each, any_of, all_of.

Real-World Outcome

struct Person { std::string name; int age; };
std::vector<Person> people = {{"Alice", 30}, {"Bob", 25}, {"Charlie", 35}};

// Task 1: Sort by age.
// std::sort(...);

// Task 2: Find first person older than 30.
// auto it = std::find_if(...);

// Task 3: Count how many people are younger than 30.
// int count = std::count_if(...);

// Task 4: Create a vector of just the names.
// std::vector<std::string> names;
// std::transform(...);

// Task 5: Remove everyone named "Bob".
// people.erase(std::remove_if(...), people.end());

Implementation Guide

  1. Reproduce the simplest happy-path scenario.
  2. Build the smallest working version of the core feature.
  3. Add input validation and error handling.
  4. Add instrumentation/logging to confirm behavior.
  5. Refactor into clean modules with tests.

Milestones

  • Milestone 1: Minimal working program that runs end-to-end.
  • Milestone 2: Correct outputs for typical inputs.
  • Milestone 3: Robust handling of edge cases.
  • Milestone 4: Clean structure and documented usage.

Validation Checklist

  • Output matches the real-world outcome example
  • Handles invalid inputs safely
  • Provides clear errors and exit codes
  • Repeatable results across runs

References

  • Main guide: LEARN_CPP_STL_DEEP_DIVE.md
  • “Effective Modern C++” by Scott Meyers