Unfortunately, that is not the answer.
std::list iterators are not random access so they can't be used with
std::sort.
The most obvious solution is to make the list being sorted an invariant of the class, in which case
Section::AddStudent is the place for things to happen, whether it's calling sort after inserting an element or just inserting an element in the right place to begin with.
Either way, you need some way to compare students (and it's not clear from the code given whether that exists or not.)
Either:
1 2 3 4 5
|
void Section::addStudent(Student s) {
students.push_back ( s );
students.sort(comparison_func);
}
}
|
where
comparison_func is some user defined function for comparing one
Student to another.
or
1 2 3 4 5
|
void Section::addStudent(Student s) {
auto insertion_point = std::lower_bound(students.begin(), students.end(), s);
students.insert(insertion_point, s);
}
}
|
where there is an appropriate overload for
operator<(const Student&, const Student&). Also note that there is an overload for
lower_bound that takes 4 arguments where the last argument is a function object that designates the comparison function similar to the argument for
list::sort.