- Analyzing strings
- Comparing Grading Schemes
- Classifying Students, Revisited
- Algorithms, Containers, and Iterators
- Details
Classifying Students, Revisited
In a previous chapter, we looked at the problem of copying records with failing grades into a separate vector and then removing those records from the existing vector. The obvious, easy approach to this problem proved to have abysmal performance as the input size grew. We went on to show how to solve the performance problem by using a list instead of a vector, but we also promised to revisit the problem and show an algorithmic solution that would perform similarly to the revised data structure.
We can use the algorithm library to demonstrate two other solutions. The first is slightly slower because it uses a pair of library algorithms and visits every element twice. We can do better by using a more specialized library algorithm that will let us solve the problem in a single pass.
A Two-Pass Solution
Our first approach will use a strategy similar to the one that we used when we wanted only the nonzero homework grades. In that case, we didn't want to change homework itself, so we used remove_copy to put copies of the nonzero homework grades into a separate vector. In our current problem, we need both to copy and "remove" the nonzero elements:
vector<Student_info> extract_fails(vector<Student_info>& students) { vector<Student_info> fail; remove_copy_if(students.begin(), students.end(), back_inserter(fail), pgrade); students.erase(remove_if(students.begin(), students.end(), fgrade), students.end()); return fail; }
The interface to the program is identical to that which presented the obvious vector-based solution that used iterators instead of indices. As in that solution, we'll use the vector that we were passed to hold grades for students who passed, and we'll define fail to hold the failing grades. There the similarities end.
In the original program, we used an iterator named iter to march through the container, copying the records with failing grades into fail and using the erase member to erase them from students. This time, we use the remove_copy_if function to copy the failing grades into fail. That function operates as did the remove_copy function that we used, except that it uses a predicate as its test rather than a value. We give it a predicate that inverts the result of calling fgrade:
bool pgrade(const Student_info& s) { return !fgrade(s); }
When we pass a predicate to remove_copy_if, we are asking it to "remove" each element that satisfies the predicate. In this context, "removing" an element means not copying it, so we copy only those elements that do not satisfy the predicate. Therefore, passing pgrade to remove_copy_if copies only the student records with failing grades.
The next statement is somewhat complicated. First, we call remove_if to "remove" the elements that correspond to failing grades. Again, nothing is actually "removed." Instead, remove_if copies all the elements that do not satisfy the predicatein this case, all the student records with passing grades.
This call is tricky to understand because remove_if uses the same sequence as its source and destination. What it really does is copy to the beginning of the sequence the elements that don't meet the predicate. For example, suppose that we started with seven students with grades as follows:
pass pass students.begin() fail fail pass fail pass students.end()
Then the call to remove_if would leave the first two records untouched because they're already in the right places. It would "remove" the next two records by treating them as free space to be overwritten by the next records that should be kept. So, when it sees the fifth record, which represents a student who passed, it would copy that record into the now free position that used to hold the first of the "removed" failing records, and so on:
pass pass students.begin() fail fail pass fail pass students.end()
The result in this case would be to copy the four passing records to the beginning of the sequence, leaving the remaining three records untouched. So that we can know how much of the sequence is still relevant, remove_if returns an iterator that refers to one past the last element that it did not "remove":
pass pass pass pass pass fail pass students.begin() result of remove_if students.end()
Next, we need to erase these unneeded records from students. We have not used this version of erase before. It takes two iterators and erases all the elements in the range delimited by those iterators. If we erase the elements between the iterator returned from the call to remove_if and students.end(), we are left with just the passing records:
pass pass pass pass students.begin() students.end()
A Single-Pass Solution
Our first algorithmic solution performs pretty well, but we should be able to do slightly better. The reason is that the previous solution calculates the grade for every element in students twice: once from remove_copy_if and a second time from remove_if.
Although there is no library algorithm that does exactly what we want, there is one that approaches our problem from a different angle: It takes a sequence and rearranges its elements so that the ones that satisfy a predicate precede the ones that do not satisfy it.
There are really two versions of this algorithm, which are named partition and stable_partition. The difference is that partition might rearrange the elements within each category, and stable_partition keeps them in the same order aside from the partitioning. So, for example, if the student names were already in alphabetical order and we wanted to keep them that way within each category, we would need to use stable_partition rather than partition.
Each of these algorithms returns an iterator that represents the first element of the second section. Therefore, we can extract the failing grades this way:
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; }
To understand what is going on here, let's start with our hypothetical input data again:
pass |
pass |
fail |
fail |
pass |
fail |
pass |
students.begin() |
|
|
|
|
|
students.end() |
After calling stable_partition, we would have:
pass |
pass |
pass |
pass |
fail |
fail |
fail |
students.begin() |
|
|
iter |
|
|
students.end() |
We construct fail from a copy of the failing records, which are the ones in the range [iter, students.end()), and then erase those elements from students.
When we ran our algorithm-based solutions, they had roughly the same overall performance as the list-based solution. As expected, once the input was large enough, the algorithm and list-based solutions were substantially better than the vector solution that used erase. The two algorithmic solutions are good enough that the time consumed by the input library dominated the timings for input files up to about 75,000 records. To compare the effects of the two strategies in extract_fails, we separately analyzed the performance of just this portion of the program. Our timings confirmed that the one-pass algorithm ran about twice as fast as the two-pass solution.