algorithm - Templates with <class Comparable> in C++ -
i attempting compare algorithms. not familiar c++. want create main include code below header. don't understand "template class comparable" though.
#include <vector> using namespace std; template <class comparable> void selectionsort(vector<comparable> & nums, int low, int high) { (int = low; <= high-1; i++) { int indexofmin = i; // traverse list (int j = i+1; j <= high; j++) { // find index of if (nums[j] < nums[indexofmin]) { // next smallest item indexofmin = j; } } comparable temp = nums[i]; // swap next smallest nums[i] = nums[indexofmin]; // item correct nums[indexofmin] = temp; // position } } template <class comparable> void selectionsort(vector<comparable> & nums) { selectionsort(nums, 0, nums.size()-1); }
your main sort function there looks (snipping "template" part now):
void selectionsort(vector<comparable> & nums) { selectionsort(nums, 0, nums.size()-1); }
looks normal sort function acts on vector of comparables. comparable? imagine if "comparable" nothing more alias "int" (it's not, imagine). you'd have this:
void selectionsort(vector<int> & nums) { selectionsort(nums, 0, nums.size()-1); }
this ordinary c++ code. declares , defines function sorts vector of ints. pretty straightforward.
comparable doesn't have standard meaning that. term invented code in question. declared text template <class comparable>
, approximately way variable declared. "type variable". ordinary variable represents 1 of many values; type variable represents 1 of many types.
template <class comparable> void selectionsort(vector<comparable> & nums) { selectionsort(nums, 0, nums.size()-1); }
this code declares comparable not automatically int, or float, or std::string, rather may type @ all. use function must specify type want when call function. can explicitly:
std::vector<int> someints; selectionsort<int>(someints);
(and make "comparable" mean "int" after all, within 1 call.)
or can leave out specification , hope compiler figure out:
std::vector<int> someints; selectionsort(someints);
and can use same template different types as want; not "spent" in sense 1 use:
std::vector<int> someints, moreints; std::vector<float> somefloats; selectionsort(someints); selectionsort(somefloats); selectionsort(moreints);
for simple purpose this, can imagine selectionsort function works on many types, not one. not function. whole family of potential functions, of may instantiated compiler. code above calls selectionsort 3 times, 2 comparable types, , behind scenes creates 2 actual functions.
i've been talking comparable variable, can't vary within instance of template. can't comparable=float
within selectionsort<int>
or that. varies 1 instance of template another, not within 1 instance. when template instantiated real function, comparable replaced type specified , forgotten; function doesn't "know" instance of template. it's function happens have angle brackets in name. think.
there indeed powerful, complicated, mind-bending things can done templates. don't need know purpose.
one more important basic point, though, there template classes. std::vector 1 of them. work in analogous way template functions selectionsort: header <vector>
declares vector template once types, can std::vector<int>
, later std::vector<someclassimade>
, on, , thereby automatically instantiate 2 (or more) actual classes. these classes work c++ vector supposed to, each 1 know how handle own specified element type, , not understand other.
Comments
Post a Comment