Finding a Student
Lucy has come to complain: the teacher added up her points wrong. She should have 35, not 27. The program needs a function that fixes a student's score by name. But first, it has to find the student.
That's a job for std::find_if from the lesson on algorithms. It goes through a range and returns an iterator to the first element for which a lambda returns true. Here, the lambda compares each student's name with the name you're looking for. That name is a variable outside the lambda, so the lambda has to capture it: write the variable's name between its square brackets, [name]. When std::find_if finds nothing, it returns end(), the place after the last element. An example with a timetable:
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
struct Subject {
std::string name;
int lessons;
};
void printLessons(const std::vector<Subject>& timetable, const std::string& wanted) {
auto it = std::find_if(timetable.begin(), timetable.end(), [wanted](const Subject& subject) { return subject.name == wanted; });
if (it == timetable.end()) {
std::cout << wanted << " is not on the timetable." << std::endl;
} else {
std::cout << it->name << ": " << it->lessons << " lessons a week" << std::endl;
}
}
int main() {
std::vector<Subject> timetable = {{"Math", 4}, {"Physics", 2}, {"History", 2}};
printLessons(timetable, "Physics");
printLessons(timetable, "Chemistry");
return 0;
}Physics: 2 lessons a week
Chemistry is not on the timetable.Strings compare exactly: "lucy" is not the same as "Lucy". Without the capture, the compiler reports:
main.cpp:79:114: error: variable 'name' cannot be implicitly captured in a lambda with no capture-default specifiedThe iterator points into the vector, so it->score = newScore; changes the score of the student in the vector itself. That's why fixScore() gets the vector as a reference without const.
The order of the calls in main() matters. Lucy scored 35 points in the test, and she gets the 3 points for the broken question like everyone else. So the fix goes before the call of addPoints(students, 3). After it, the fix would overwrite Lucy's bonus, and she'd have just 35 points.
And what if the teacher types a name that isn't in the class? Max, for example, is in the class next door. Then it is end(), which points to no student, and it->score would write into memory after the last element. Here, the program even prints its results as if nothing had happened, which makes such a bug hard to find. That's why fixScore() checks the iterator first.
Instructions
- 1
Under
addPoints(), write a functionvoid fixScore(std::vector<Student>& students, const std::string& name, int newScore)that finds the student callednamewithstd::find_ifand a lambda that capturesname, and sets that student's score tonewScorethrough the iterator. - 2
In
main(), above the call ofaddPoints(), fix Lucy's score:fixScore(students, "Lucy", 35);. Lucy now has 35 + 3 = 38 points, and the results end like this:Lucy: 38 points, grade B Average score: 38.2 Best: Finn (50 points) Ranking: Finn, Adam, Emma, Lucy, JackIf Lucy has 35 points, you call the fix after
addPoints(). - 3
Add a check to
fixScore(): if the iterator isstudents.end(), printMax is not in the class.with the name the function got instead ofMax, and don't change any score. - 4
Try it out: under Lucy's fix, add
fixScore(students, "Max", 40);. This appears above the results:Max is not in the class.