This checkpoint builds a report the tracker will actually need: "pending tasks of at least N minutes, longest first". It has two separate jobs. Selection decides which tasks appear, with a predicate. Ordering decides where they appear, with a comparator. Keeping the jobs apart makes each one easy to test: a selection bug changes which rows appear, an ordering bug changes their order, and you can tell which from the output.
The report must also leave the source alone. It builds its own vector, a snapshot that owns copies of the selected rows, and sorts that. Sorting a snapshot cannot reorder the tracker, and a snapshot cannot dangle when the tracker later changes, because it shares nothing with it. That makes the report safe to call between any two updates.
Predict and run
The loop selects, and then std::sort orders the selection. What is printed, and what is left in values?
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> values{10, 40, 25};
std::vector<int> selected;
for (int value : values) { if (value >= 25) { selected.push_back(value); } }
std::sort(selected.begin(), selected.end());
for (int value : selected) { std::cout << value << "\n"; }
return 0;
}Check your prediction
25
40std::sort with no comparator uses <, so the result is ascending. values still holds 10, 40, 25 in that order: push_back copied each selected int into selected, and the sort only touched the copy.
What you will use
- A
const std::vector<Row>¶meter, so the function can read the source but not change it. - A value-capturing lambda that keeps the threshold:
[threshold](const Row& row) { ... }. - A new result vector filled with
push_back, or withstd::copy_ifandstd::back_inserter. - A strict comparator with a unique last key, so ties have a defined order.
- Boundary inputs: a task exactly at the threshold, a threshold above every task, a done task that would otherwise match, and an empty source.
Plan before you type
- Write the selection rule in words first: not done, and minutes greater than or equal to the threshold.
- Create an empty
std::vector<Row> resultand copy in every row that passes the rule. - Sort
resultby minutes descending, then ID ascending. - Return
resultby value. Returning a local vector moves it out; nothing is copied twice. - Test with threshold 25, threshold 41 and an empty source, and print the source afterward to show it is unchanged.
Prove it can fail
Break each half on purpose. Change row.minutes >= threshold to > and the threshold-25 report must lose tasks 2 and 6. Remove !row.done && and the done task 4 must appear. Delete the ID tie-breaker and replace it with return false;: the output may not change on your machine, which is exactly why a tie-breaker must be written rather than observed. Swap the IDs of tasks 2 and 6 in the input too, and check the report still lists the lower ID first.
Mistakes worth checking for
- The tracker's own order changed after a report: the function sorted its parameter by reference. Sort a new vector, or take the parameter by value.
- A task exactly at the threshold is missing: the predicate used
>instead of>=. - Done tasks appear: the predicate tested only minutes. Each condition in the rule must be in the code.
- Equal durations swap between runs or compilers: the comparator has no unique final key, so equal rows are in unspecified order.
- A crash or garbage with many equal values: the comparator used
>=or<=, which breaks the strict ordering contract and is undefined behavior.
Practice
Your turn
Write std::vector<Row> pending_at_least(const std::vector<Row>& rows, int threshold) for struct Row { int id; int minutes; bool done; };. Use the source {1, 10, false}, {6, 25, false}, {4, 40, true}, {3, 40, false}, {2, 25, false} and print:
threshold 25: 3:40 2:25 6:25
threshold 41: (none)
empty source: (none)
source unchanged: 1 6 4 3 2#include <algorithm>
#include <iostream>
#include <vector>
struct Row { int id; int minutes; bool done; };
// Write pending_at_least here.
int main() {
const std::vector<Row> rows{{1, 10, false}, {6, 25, false}, {4, 40, true}, {3, 40, false}, {2, 25, false}};
return 0;
}Hint 1: the idea
Selection and ordering are separate operations. Collect the matches into a new vector first, and only then sort that vector.
Hint 2: a first step
std::copy_if(rows.begin(), rows.end(), std::back_inserter(result), predicate) appends every matching row to result; it needs <iterator>. A plain loop with if and push_back is equally good.
Show one solution
#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>
struct Row { int id; int minutes; bool done; };
std::vector<Row> pending_at_least(const std::vector<Row>& rows, int threshold) {
std::vector<Row> result;
std::copy_if(rows.begin(), rows.end(), std::back_inserter(result),
[threshold](const Row& row) { return !row.done && row.minutes >= threshold; });
std::sort(result.begin(), result.end(), [](const Row& a, const Row& b) {
if (a.minutes != b.minutes) { return a.minutes > b.minutes; }
return a.id < b.id;
});
return result;
}
void print(const char* label, const std::vector<Row>& rows) {
std::cout << label << ":";
if (rows.empty()) { std::cout << " (none)"; }
for (const Row& row : rows) { std::cout << ' ' << row.id << ':' << row.minutes; }
std::cout << "\n";
}
int main() {
const std::vector<Row> rows{{1, 10, false}, {6, 25, false}, {4, 40, true}, {3, 40, false}, {2, 25, false}};
print("threshold 25", pending_at_least(rows, 25));
print("threshold 41", pending_at_least(rows, 41));
print("empty source", pending_at_least({}, 25));
std::cout << "source unchanged:";
for (const Row& row : rows) { std::cout << ' ' << row.id; }
std::cout << "\n";
return 0;
}threshold 25: 3:40 2:25 6:25
threshold 41: (none)
empty source: (none)
source unchanged: 1 6 4 3 2Task 4 has 40 minutes but is done, so the predicate excludes it; tasks 2 and 6 sit exactly at the threshold and are included, in ID order because they tie on minutes. The empty source and the threshold above every task both give an empty vector naturally, with no special case. Day 4 turns to operations that can fail in more than one way, and how to report which way.
Completion is saved in this browser only. It does not measure mastery.