Minni
March 30th, 2002, 03:22 AM
I have list which stores an instances of a class. All the data in the class is
private and has member functions to access the data.
I have to sort the list on one of the class fields. I am not supposed to write any bubble sort, heap sort or anyother kind of sort routines.
I am supposed to use the appropriate sort member functions of the container class.
Iam giving an outline of the program here
#include <iostream>
#include <string>
#include<list>
#include<fstream>
#include <algorithm>
using namespace std;
class School{
public:
School(){
}
void setname(string);
void setcity(string);
string getname();
string getcity();
private:
string schoolName;
string city;
};
void School::setname( string temp)
{
schoolName =temp;
}
string School::getname()
{return schoolName;
}
void School::setcity( string temp)
{
city =temp;
}
string School::getcity()
{return city;
}
typedef list<School>school;
school sv;
school::iterator s_it;
struct NameCompare {
bool operator()(const School& a,const School& b) {
return a.getname() << b.getname(); // based on
//names only
}
};
NameCompare nameComp;
void output(const School& record)
{
cout << record.getname()<< " " <<
record.getcity()
<< "\n";
}
int main()
{
string temp;
School mysch;
for(int count=1; count<4; count++)
{
cout<<"First name:"<<endl;
getline(cin, temp);
mysch.setname(temp);
cout<<"Last name:"<<endl;
getline(cin, temp);
mysch.setcity(temp);
sv.push_back(mysch);
}
sort(sv.begin(), sv.end(), nameComp);
for(s_it=sv.begin(); s_it!=sv.end();s_it++)
output(*s_it);
return(0);
}
Can any body help me out please,this is urgent
private and has member functions to access the data.
I have to sort the list on one of the class fields. I am not supposed to write any bubble sort, heap sort or anyother kind of sort routines.
I am supposed to use the appropriate sort member functions of the container class.
Iam giving an outline of the program here
#include <iostream>
#include <string>
#include<list>
#include<fstream>
#include <algorithm>
using namespace std;
class School{
public:
School(){
}
void setname(string);
void setcity(string);
string getname();
string getcity();
private:
string schoolName;
string city;
};
void School::setname( string temp)
{
schoolName =temp;
}
string School::getname()
{return schoolName;
}
void School::setcity( string temp)
{
city =temp;
}
string School::getcity()
{return city;
}
typedef list<School>school;
school sv;
school::iterator s_it;
struct NameCompare {
bool operator()(const School& a,const School& b) {
return a.getname() << b.getname(); // based on
//names only
}
};
NameCompare nameComp;
void output(const School& record)
{
cout << record.getname()<< " " <<
record.getcity()
<< "\n";
}
int main()
{
string temp;
School mysch;
for(int count=1; count<4; count++)
{
cout<<"First name:"<<endl;
getline(cin, temp);
mysch.setname(temp);
cout<<"Last name:"<<endl;
getline(cin, temp);
mysch.setcity(temp);
sv.push_back(mysch);
}
sort(sv.begin(), sv.end(), nameComp);
for(s_it=sv.begin(); s_it!=sv.end();s_it++)
output(*s_it);
return(0);
}
Can any body help me out please,this is urgent