Exercise: Search a User By Lastname (std::find_if)

Description

In the previous exercise, Exercise: Search a User By Lastname, we used the original (C) search implementation, which has a number of shortcomings,

  • uses a manual loop over the C array

  • is not quite readable

Fix that, and replace the manual search code with std::find_if to do the same.

Testing requires no additional code (we did not change anything that is user-visible). The test program from the previous exercise is sufficient - if that works, our change did not break anything.

#include "userdb.h"
#include <iostream>
#include <cassert>

int main()
{
    UserDB db;

    // setup db to have something to search for
    // ----------------------------------------
    db.insert(User("Philipp", "Lichtenberger", 35));
    db.insert(User("Joerg", "Faschingbauer", 55));
    db.insert(User("Caro", "Faschingbauer", 24));

    // test the search
    // ---------------

    // we find the *first* user if it exists
    User found = db.search_by_lastname("Faschingbauer");
    assert(found.firstname() == "Joerg");
    assert(found.lastname() == "Faschingbauer");
    assert(found.age() == 55);

    // lacking knowledge of exception handling, we have to return an
    // invalid user when none exists.
    User notfound = db.search_by_lastname("DoesNotExist");
    assert(!notfound.isvalid());

    return 0;
}