I am working through the examples in Accelerated C++.
One of the problems asks the reader to copy records for students who pass exams into the beginning of a vector called students. fgrade
is a function defined elsewhere which returns failing students.
Then one has to use the resize function to remove the extra elements from students so it only contains those who pass.
I have tried this code but it does not work. Could anyone tell me if the fault lies in the code below?
#include "stdafx.h"
#include <vector>
#include "Student_info.h"
#include "grade.h"
using std::vector;
// second try: correct but potentially slow
vector<Student_info> extract_fails(vector<Student_info>& students)
{
vector<Student_info> fail;
#ifdef _MSC_VER
std::vector<Student_info>::size_type i = 0;
std::vector<Student_info>::size_type count = 0;
#else
vector<Student_info>::size_type i = 0;
vector<Student_info>::size_type count = 0;
#endif
while (i != students.size()) {
if (!fgrade(students[i])) {
students.insert(students.begin(), students[i++]);
count++;
}
i++;
}
students.resize(count);
return students;
}