Hello all,
I know there is a "sort" method in the STL for list. However I am not sure how to use it, and my research was unable to help me out. What I am trying to do is sort my master list by the averages, lowest to highest.
Can anyone give me clues as how to use the STL sort to do this?
Thanks
/*******************************************************************************************
Devang N. Joshi *
CSCI 271 *
Assignment Three *
February 19th, 2011 *
STL Double Linked List *
*
The purpose of this assignment is to gain experiance using C++ built in List STL *
/******************************************************************************************/
#include <iostream> // |
#include <iomanip> // |
#include <fstream> // |
#include <cstdlib> // |
#include <cstring> // |
#include <cmath> // |
#include <list> // |
using namespace std; // |
//-----------------------------------------------------------------------------------------|
struct cProg3 // |
{ // |
char lname[21]; // |
float avg; // |
int gSize; // |
cProg3(); // |
void Read_m(fstream &Infile); // |
void Read_g(fstream &Infile); // |
// |
// |
}; // |
/***Functions**/ // |
//-----------------------------------------------------------------------------------------|
cProg3::cProg3() // |
{ // |
strcpy(lname,""); // |
avg = 0; // |
gSize = 0; // |
} // |
//-----------------------------------------------------------------------------------------|
void cProg3::Read_m(fstream &Infile) // |
{ Infile>>lname>>avg; } // |
//-----------------------------------------------------------------------------------------|
void cProg3::Read_g(fstream &Infile) // |
{ Infile>>lname>>gSize; } // |
//-----------------------------------------------------------------------------------------|
/***MAIN***/ // |
//-----------------------------------------------------------------------------------------|
int main() // |
{ // |
//-----------------------------------------------------------------------------------------|
/***File Handlers***/ // |
fstream Infile; // |
fstream Outfile; // |
// |
/***Class Decleration***/ // |
cProg3 Prog3; // |
// |
/***List Decleration***/ // |
list<cProg3>theList; //the list for master.dat // |
list<cProg3>groupList; //the list for groups.dat // |
list<cProg3>::iterator it_m; //master list iterator // |
list<cProg3>::iterator it_g; //groups list iterator // |
list<cProg3>::iterator loopCount_m; //for read & write loops in master list // |
list<cProg3>::iterator loopCount_g; //for read & write loops in groups list // |
//-----------------------------------------------------------------------------------------|
/***Open Master Data and error check***/ // |
Infile.open("master.dat",ios::in); // |
if(!Infile) // |
{ // |
cerr<<"FILE NOT FOUND!!!!!!!!!!!!!"<<endl; // |
exit(1); // |
// |
} // |
else // |
{ // |
// |
cout<<"Master Data File Found & Opened"<<endl; // |
// |
/***Load Data into List***/ // |
while(!Infile.eof()) // |
{ // |
Prog3.Read_m(Infile); // |
theList.push_back(Prog3); // |
// |
};//while // |
// |
cout<<"Master Data File Loaded into List"<<endl<<endl; // |
Infile.close(); // |
// |
} // |
//-----------------------------------------------------------------------------------------|
/***Open Groups Data and error check***/ // |
Infile.open("groups.dat",ios::in); // |
if(!Infile) // |
{ // |
cerr<<"FILE NOT FOUND!!!!!!!!!!!!!"<<endl; // |
exit(1); // |
// |
} // |
else // |
{ // |
// |
cout<<"Groups Data File Found & Opened"<<endl; // |
// |
/***Load Data into Array***/ // |
while(!Infile.eof()) // |
{ // |
Prog3.Read_g(Infile); // |
groupList.push_back(Prog3); // |
// |
};//while // |
// |
cout<<"Groups Data File Loaded into List"<<endl<<endl; // |
Infile.close(); // |
// |
} // |
//-----------------------------------------------------------------------------------------|
/***Sort List***/
it_m = theList.begin();
loopCount_m = theList.end();
theList.sort();
// |
//-----------------------------------------------------------------------------------------|
/***Print Lists Back Out**/ // |
Outfile.open("output.dat",ios::out); // |
//-----------------------------------------------------------------------------------------|
/***Print Master List***/ // |
// |
loopCount_m = theList.end(); //sent loop counter to end of list // |
loopCount_m--; //move one position back for proper output // |
it_m = theList.begin(); //set iterator to begining of list // |
// |
while(it_m != loopCount_m) // |
{ // |
cout<<it_m->lname<<" "<<it_m->avg<<endl; // |
Outfile<<it_m->lname<<" "<<it_m->avg<<endl; // |
it_m++; // |
// |
};//end loop // |
cout<<endl<<endl; // |
Outfile<<endl<<endl; // |
//-----------------------------------------------------------------------------------------|
/***Print Group List***/ // |
// |
loopCount_g = groupList.end(); //sent loop counter to end of list // |
loopCount_g--; //move one position back for proper output // |
it_g = groupList.begin(); //set iterator to begining of list // |
// |
while(it_g != loopCount_g) // |
{ // |
cout<<it_g->lname<<" "<<it_g->gSize<<endl; // |
Outfile<<it_g->lname<<" "<<it_g->gSize<<endl; // |
it_g++; // |
// |
};//end loop // |
cout<<endl<<endl; // |
Outfile<<endl<<endl; // |
//-----------------------------------------------------------------------------------------|
Outfile.close(); // |
// |
return 0; // |
// |
}//end main // |
/******************************************************************************************/