Hi, I'm trying to make a very basic selection sort alogirithm into a template function:
The obvious link time error is that the template has no way of determining what type "T lowest" should be. I know it should be the same type as (*begin), but is there any way to use that information to make a variable of the proper type? One quick workaround I'd though of was to have the caller pass the value of the first element, but if possible I'd like to follow the format of the algorithm library.Code:template<class _RI> void selectionSort(_RI begin, _RI end) { T lowest; // stores lowest value that has been compared for(_RI sortedElems = begin; sortedElems != end - 1; sortedElems++) { lowest = *sortedElems; // lowest equals first unsorted element for(_RI iter = sortedElems + 1; iter != end; iter++) { if(lowest > *iter) // { swap(lowest, *iter); } }//end for iter *sortedElems = lowest; }//end for sortedElems return; }//end selectionSort()
Thank you



LinkBack URL
About LinkBacks


