Information about cookies on this page

Our website uses cookies to ensure the functionality of the website, to collect and analyze information about the performance and usage of the website. Privacy Policy

  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in
  
  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in

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 specified

The 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. 1

    Under addPoints(), write a function void fixScore(std::vector<Student>& students, const std::string& name, int newScore) that finds the student called name with std::find_if and a lambda that captures name, and sets that student's score to newScore through the iterator.

  2. 2

    In main(), above the call of addPoints(), 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, Jack

    If Lucy has 35 points, you call the fix after addPoints().

  3. 3

    Add a check to fixScore(): if the iterator is students.end(), print Max is not in the class. with the name the function got instead of Max, and don't change any score.

  4. 4

    Try it out: under Lucy's fix, add fixScore(students, "Max", 40);. This appears above the results:

    Max is not in the class.
Back

7/8

  1. 1. Test Results
  2. 2. Grades
  3. 3. Class Average
  4. 4. Top Score
  5. 5. Ranking
  6. 6. Bonus Points
  7. 7. Finding a Student
  8. 8. Congratulations!
Next

Try it yourself

Do this exercise right here in your browser. No account needed.