Reputation: 124
I am doing Sort a List Of Student via: 1.Via Name 2.Via Date Of Birth Both only differ type of compare,the rest is the same. Because I do not want duplicate code I have used template type to do this:
class Student
{
Date date; // Date is a class
Name name; // Name is a class
public:
Date getDate() { return date; }
Name getName() { return name; }
};
class ListOfStudent
{
vector<Student> List;
public:
bool CompareDate(Date &d1, Date &d2); // define d1<d2
bool CompareName(Name &n1, Name &n2); // define n1<n2
void SortViaName()
{
for (int i = 0; i < List.size(); ++i)
int min = i;
for (int j = i + 1; j < List.size(); ++j)
{
if (CompareName(List[j].getName(), List[min].getName()))
min = j;
}
if (min != i)
swap(List[i], List[min]);
}
void SortViaDate()
{
for (int i = 0; i < List.size(); ++i)
int min = i;
for (int j = i + 1; j < List.size(); ++j)
{
if (CompareName(List[j].getDate(), List[min].getDate()))
min = j;
}
if (min != i)
swap(List[i], List[min]);
}
// Because i do not want to duplicate code so I do:
template <typename T>
void Sort(bool (*f)(T, T), T (*t)())
{
for (int i = 0; i < List.size(); ++i)
int min = i;
for (int j = i + 1; j < List.size(); ++j)
{
if ((*f)(DS[j].(*t)(), List[i].(*t)()))
min = j;
}
if (min != i)
swap(List[i], List[min]);
}
};
It has something wrong at template function so I need your supports.
Upvotes: 2
Views: 82
Reputation: 10740
In order to make things generic, the simplest way to set things up is to make the CompareThing
function optionally take a Student&
instead of a Date
or Name
.
Note: CompareDate
and CompareName
should be either static or global, that way you can pass them around as function pointers.
// We'll keep these
// Also they should either be static or global
bool CompareDate(Date &d1,Date &d2); //define d1<d2
bool CompareName(Name &n1,Name &n2); //define n1<n2
// Because these can both be used with Student, it's easy to template over them
bool CompareDate(Student &s1, Student &s2) {
return CompareDate(s1.getDate(), s2.getDate());
}
bool CompareName(Student &s1, Student &s2) {
return CompareName(s1.getName(), s2.getName());
}
Then, we can write Sort
like this:
void Sort(bool(*compare)(Student&, Student&)) {
std::sort(List.begin(), List.end(), compare);
}
void SortViaName() {
Sort(CompareName);
}
void SortViaDate() {
Sort(CompareDate);
}
We can write Sort
to take the function as a parameter, and then use your selection sort instead of std::sort
:
void Sort(bool (*compare)(Student&, Student&))
{
for (int i = 0; i < List.size(); ++i) {
int min = i;
for (int j = i + 1; j < List.size(); ++j)
{
if (compare(List[j].getName(), List[min].getName()))
min = j;
}
if (min != i)
swap(List[i], List[min]);
}
}
Upvotes: 1