   Templated algorthim question
  I have been playing around with making some algorthims in template form as to support a range of containers and element types. For testing I decided to use bubble sort, however I cant seem to get the optimal solution from the usage pov.Idealy Id like to be able to use it like so, to sort items from smallest to largest using there > operator:BubbleSort(myContainer.begin(), myContainer.end())The probelm is it seems I need to explicitly provide the element type, eg:BubbleSort<int>(myContainer.begin(), myContainer.end())Is there some way to not have to provide that template argument?    template<typename T> bool GreaterThan(const T &a, const T &b)    {   return a > b;    }    //sort smallest to largest using the elements > operator by default    template<typename Element, typename Iterator>    void BubbleSort(Iterator begin, Iterator end, bool(*comp)(const Element&,const Element&)=GreaterThan<Element>)    {   if(begin==end)return;   Iterator it1, it2;   bool swapped = true;   while(swapped)   {  swapped = false;  it2 = begin;  it1 = it2++;  while(it2 != end)  { if(comp(*it1, *it2)) {swapped = true;Element tmp  = *it1;*it1 = *it2;*it2 = tmp; } ++it1;++it2;  }  --end;   }    }
  You can use std::iterator_traits<IteratorType>::value_type to get the type that an iterator refers to.   ;  Consider making the comparator a template argument. This allows for functors to be used for comparisons, which is often done. I believe that some compilers can actually inline the comparison code in a functor (if its short enough).You can avoid explicitly using the "Element" type in the innermost part of your code by using std::swap() rather than writing the code out by hand. std::swap() may actually be more efficient, for example using std::swap() with a vector is just a pointer swap, where as your method would involve copies.   ;  Or, now that I think of it, std::iter_swap().   