Need help with list sorting

This is a discussion on Need help with list sorting within the C++ Programming forums, part of the General Programming Boards category; Okay so previously, I attempted to sort of create a generic type of program. I did this by using typedef ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    110

    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
    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, record.name.size());
    		studs.push_back(record);
    	}
    
                    //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(), ' ');
    
    		try{
    			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
    Code:
    bool compare(const Student_info& x, const Student_info& y){
    	return x.name < y.name;
    }
    
    std::istream& read(std::istream& source, Student_info& student){
    	source >> student.name >> student.midterm >> student.final;
    
    	read_hw(source, student.hw);
    
    	return source;
    }
    
    
    std::istream& read_hw(std::istream& source, homework& hw){
    	double x;
    
    	if(source){
    		hw.clear();
    
    	while(source >> x){
    		hw.push_back(x);
    	}
    
    	source.clear();
    	
    	}
    
    	return source;
    
    }

    Thank you for your help!

  2. #2
    dra
    dra is offline
    Weak. dra's Avatar
    Join Date
    Apr 2005
    Posts
    166
    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, 01:46 PM
  3. Replies: 6
    Last Post: 03-02-2005, 01:45 AM
  4. compiler build error
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-30-2003, 09:16 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21