hey ik ben aant proberen te snappen hoe je een function object maakt.
tot nu toe heb ik :
ik krijg hier alleen als error uit:
no matching function for call to `quick_sort(int[20], int*, bool (*&)(const int&, const int&))'
die ik niet echt snap
Kees
tot nu toe heb ik :
Code:
#include <iostream>
bool sortnumint(const int& S1, const int& S2)
{
return S1 < S2;
}
template <typename T>
void quick_sort(T& begin, T& end, bool (*functie)(const int&,const int&)) {
if (functie(begin+1<end)) {
if (begin+2==end) {
if (functie(begin[1]<begin[0]))
std::swap(begin[1], begin[0]);
}
else {
int low(0);
int high(end-begin-1);
int middle((low+high)/2);
if (functie(begin[middle],begin[low]))
std::swap(begin[middle], begin[low]);
if (functie(begin[high]<begin[low]))
std::swap(begin[high], begin[low]);
if (functie(begin[high]<begin[middle]))
std::swap(begin[high], begin[middle]);
int pivot(begin[middle]);
std::swap(begin[middle], begin[high-1]);
int i(low);
int j(high-1);
while (true) {
while( begin[++i]<pivot) /* niets */;
while( pivot<begin[--j]) /* niets */;
if (i<j)
std::swap(begin[i], begin[j]);
else
break;
}
std::swap(begin[i], begin[high-1]);
quick_sort(begin+low, begin+i, functie);
quick_sort(begin+i+1, begin+high+1, functie);
}
}
}
int main() {
const int aantal(20);
int a[aantal];
for (int i(0); i<aantal; ++i) {
a[i]=std::rand();
}
bool (*foo)(const int&,const int&);
foo = &sortnumint;
quick_sort(a, a+aantal, foo);
for (int i(0); i<aantal; ++i) {
std::cout<<a[i]<<std::endl;
}
std::cin.get();
}
ik krijg hier alleen als error uit:
no matching function for call to `quick_sort(int[20], int*, bool (*&)(const int&, const int&))'
die ik niet echt snap
Kees