I'm playing around with templates and I was wondering why I'm getting a no matching function error using templates.
/*selection sort*/
template <typename InputIterator, typename T>
void selection_sort(InputIterator first, InputIterator last){
InputIterator min;
for(; first != last - 1; ++first){
min = first;
for(T i = (first + 1); i != last ; ++i)
{
if(*first < *min)
min = i;
}
myswap(*first, *min);
}
}
int main(){
int a[] = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
vector<int> v(a, a+10);
selection_sort(v.begin(),v.end());
}
The problem is that
typename T
which seems not to be used, can't be deduced by the compiler. You will have to specify the types explicitly:You have an undeduced template parameter T, so you need 1) move your
typename T
as the first template parameter:and 2) to qualify your call to sort as
selection_sort<int>(v.begin(), v.end());
BTW, here's a somwhat more generic implementation of selection sort, note it takes only an iterator and comparison function as template parameters, with the comparison function taking the value type that the iterator points to (this is C++11 code because of the default function template parameter, for C++98 compilers you need to have 2 overloads, with or without the comparison function)
The call to
std::min_element
is equivalent to your for loop, and theiter_swap
is equal to your own swap. The advantage of using STL algorithms is that they are much more likely to be correct (off-by-one errors in handwritten code are very common)PS: you can similarly write an insertion_sort algorithm in 2 lines using
std::upper_bound
andstd::rotate
(exercise for the reader)