Skip to content

Come Back Later

The Course
•
30 min study

Return to this page after a few days, and again about a week later. Start each visit from a blank editor, with the course closed. Recalling an idea after you have partly forgotten it is what makes it stick; rereading a solution only feels like learning. This review is extra practice outside Day 7's time estimate.

Retrieve

Without looking anything up, build a small class with a private field, a static factory returning std::optional, and a const query. Then answer these out loud or on paper:

  • Why is the constructor private, and why is there no public default constructor?
  • What does an edit have to do so that a rejected call leaves the object unchanged?
  • Who owns the strings inside a class that follows the rule of zero, and when are they freed?
  • After b = std::move(a); for a std::string, what may you still do with a?

Debug

Each fragment below has one fault. Explain what goes wrong and how to repair it. Do not run them: three are undefined behavior, and a run that "works" proves nothing. Write your answer for each before opening its hint.

C++
std::map<int, Task> tasks;
if (tasks[id].minutes() > 0) { /* ... */ } // Fault 1
Check fault 1

operator[] inserts a default value for a missing key, so a question changes the data; with the project's Task, which has no public default constructor, it does not even compile. Use find and compare with end().

C++
std::string_view first_word(const std::string& text) {
    std::string copy = text;
    return std::string_view(copy).substr(0, copy.find(' ')); // Fault 2
}
Check fault 2

the view points into copy, which is destroyed on return, so every use of the result reads freed memory. Return std::string, or view text itself, which the caller owns.

C++
std::sort(rows.begin(), rows.end(), [](const Row& a, const Row& b) {
    return a.minutes >= b.minutes; // Fault 3
});
Check fault 3

>= is true for equal elements, which breaks the strict ordering std::sort requires; the sort may read outside the vector. Use > and add the ID as a tie-breaker so equal durations have a defined order.

C++
for (auto it = values.begin(); it != values.end(); ++it) {
    if (*it < 15) { values.erase(it); } // Fault 4
}
Check fault 4

erase invalidates it, and ++it then uses it. Write it = values.erase(it); and advance only when nothing was erased, or call std::erase_if.

Transfer: predict and run

What total does this loop produce?

Predict before running C++
#include <iostream>
#include <vector>

int main() {
    std::vector<int> values{10,25,40};
    long total = 0;
    for (int value : values) { if (value >= 25) { total += value; } }
    std::cout << total << "\n";
    return 0;
}
Check your prediction
Text
65

Raise the threshold above every value and the total is 0 with no special case, which is the same answer an empty list gives. A report over "nothing that matches" should fall out of the loop naturally.

Practice

Exercise

Your turn

Without looking at Day 6, write template<class T, class Predicate, class Minutes> long total_if(const std::vector<T>& items, Predicate matches, Minutes minutes), where minutes is a second callable that returns how many minutes an item contributes. Use it with a fresh record type, struct Session { int minutes; bool reviewed; };, for two reports over the same list, and test all, none, selective and empty cases. For {30, true}, {15, false}, {45, true}, print:

Text
reviewed: 75
not reviewed: 15
all: 90
none: 0
empty: 0
Your workspace C++
#include <iostream>
#include <vector>

struct Session { int minutes; bool reviewed; };

// Write total_if here.

int main() {
    const std::vector<Session> sessions{{30, true}, {15, false}, {45, true}};
    return 0;
}
Hint 1: the idea

The caller supplies behavior; the helper owns traversal and accumulation. One callable decides which items count, the other says how much each one is worth, and neither knows about the loop.

Hint 2: a first step

Start with the first report only. Write total_if with its three template parameters and an accumulator that starts at 0, and call it with two lambdas: one that keeps reviewed sessions and one that returns a session's minutes. When reviewed: 75 prints, the other reports are only new lambdas.

Show one solution
Run the solution C++
#include <iostream>
#include <vector>

struct Session { int minutes; bool reviewed; };

template<class T, class Predicate, class Minutes>
long total_if(const std::vector<T>& items, Predicate matches, Minutes minutes) {
    long total = 0;
    for (const T& item : items) {
        if (matches(item)) { total += minutes(item); }
    }
    return total;
}

int main() {
    const std::vector<Session> sessions{{30, true}, {15, false}, {45, true}};
    auto value = [](const Session& s) { return s.minutes; };
    std::cout << "reviewed: " << total_if(sessions, [](const Session& s) { return s.reviewed; }, value) << "\n";
    std::cout << "not reviewed: " << total_if(sessions, [](const Session& s) { return !s.reviewed; }, value) << "\n";
    std::cout << "all: " << total_if(sessions, [](const Session&) { return true; }, value) << "\n";
    std::cout << "none: " << total_if(sessions, [](const Session&) { return false; }, value) << "\n";
    std::cout << "empty: " << total_if(std::vector<Session>{}, [](const Session&) { return true; }, value) << "\n";
    return 0;
}
Text
reviewed: 75
not reviewed: 15
all: 90
none: 0
empty: 0

The reviewed and not-reviewed totals add up to the "all" total, a cheap consistency check that would catch a helper that ignores its predicate. The same traversal serves every report, so a fix to it fixes all of them.

If ownership and views still feel unclear, revisit Day 5 before moving on to smart pointers or asynchronous work. If error handling is confusing, trace one malformed line and one rejected edit through the project's public results. C intermediate is a separate path; neither intermediate course requires the other.

Was this article helpful?

Your response is saved on this device.