This is Part 7 of the 7-part Exercise 6-0. Click here to see the other parts.
Exercise 6-0 (Part 7 / 7)
Relating to section 6.3.2 of the textbook (page 119-120). Building on the previous exercise the author introduces an even better “one-pass extract_fails” program which takes half the time to run in comparison to the “two-pass” version. This version of extract_fails function uses the stable_partition and erase from the <algorithm> directive.
The Project
Note that the project for this exercise is exactly the same as the one shown in my Solution to Exercise 6-0 (Part 6 / 7). Except for one file as follows:
extract_fails.cpp
#include <vector> // std::vector #include <algorithm> // std::remove_copy_if #include "Student_info.h" #include "fgrade.h" #include "pgrade.h" using std::vector; // A single-pass solution to extract the failed students // (S6.3.2/119) vector<Student_info> extract_fails(vector<Student_info>& students) { vector<Student_info>::iterator iter = stable_partition(students.begin(), students.end(), pgrade); vector<Student_info> fail(iter, students.end()); students.erase(iter, students.end()); return fail; }
Test Program
Using the same set of student grades as per Exercise 6-0 (Part 6 / 7), the result is the same as expected.
pete 100 100 100 100 100 jon 90 90 0 0 0 mary 50 50 50 50 50 anna 40 40 0 0 0 gary 80 80 0 80 0 bob 100 100 100 0 0 ken 20 88 99 44 66 jay 99 39 40 80 0 bill 20 88 0 39 0 ^Z ^Z These students have passed. bob (60) ken (65.6) pete (100) These students have failed. anna (24) bill (39.2) gary (48) jay (51.4) jon (54) mary (50)