Thread: Need help with list sorting

  1. #1
    Registered User
    Join Date
    Nov 2008

    Need help with list sorting

    Okay so previously, I attempted to sort of create a generic type of program. I did this by using typedef to define two vectors. One of structs and one of doubles. However when I changed the typedef from vectors to list within my program, and all else the same, it happened to fail on me when I attempted to sort the list.

    The error gives me something about a reverse_iterator. I don't recall reversing any iterator in my code.

    Here is my main code
    int main(){
    	typedef list<Student_info> students;
    	students studs;
    	Student_info record;
    	string::size_type maxlen = 0;
    	while(read(cin, record)){
    		maxlen = max(maxlen,;
                    //problem is here!
    	sort(studs.begin(), studs.end(), compare);
    	for(students::iterator i = studs.begin(); i != studs.end(); ++i){
    		cout << (*i).name << string(maxlen + 1 - (*i).name.size(), ' ');
    			double final_grade = grade(*i);
    			streamsize prec = cout.precision();
    			cout << setprecision(3) << final_grade << setprecision(prec);
    		catch(domain_error e){
    			cout << e.what();
    		cout << endl;
    	return 0;
    Here is the source file with the compare function
    bool compare(const Student_info& x, const Student_info& y){
    	return <;
    std::istream& read(std::istream& source, Student_info& student){
    	source >> >> student.midterm >>;
    	read_hw(source, student.hw);
    	return source;
    std::istream& read_hw(std::istream& source, homework& hw){
    	double x;
    	while(source >> x){
    	return source;

    Thank you for your help!

  2. #2
    Weak. dra's Avatar
    Join Date
    Apr 2005
    You cannot use sort() on a list because it doesn't support random access iterators. std::list has its own sort member function that you can use.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  2. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  3. Replies: 6
    Last Post: 03-02-2005, 02:45 AM
  4. compiler build error
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-30-2003, 10:16 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM