Often you can use the functors available in functional to build the resulting sort functor exclusively from standard constructs.
But there is no dereference of the T* pointer, so you have to use your own comparator.
The closest thing you can get is when your "pointer type" is not primitive, but some User-Defined-Type with operator* , which can be solved.
The following code is C ++ 11 (for using std::bind , which is simpler than std::bind1st and std::bind2nd ).
#include <vector> #include <algorithm> #include <functional> #include <iostream> // Fakes a "smart pointer" wrapper around data template <typename T> struct Ptr { Ptr(T data) : data(data) {}; const T& operator*() const { return data; } private: T data; }; int main() { std::vector<Ptr<double>> vIn; vIn.push_back(Ptr<double>(5)); vIn.push_back(Ptr<double>(2)); vIn.push_back(Ptr<double>(6)); using namespace std::placeholders; std::sort( vIn.begin(), vIn.end(), std::bind( std::less<double>(), std::bind(&Ptr<double>::operator*, _1), std::bind(&Ptr<double>::operator*, _2) ) ); std::vector<Ptr<double>>::const_iterator it = vIn.begin(), end = vIn.end(); for ( ; it != end; ++it) std::cout << ',' << **it; }
Thus, if instead of double* you have std::unique_ptr<double> or std::shared_ptr<double> , this might work:
#include <vector> #include <memory> #include <algorithm> #include <functional> #include <iostream> int main() { typedef std::unique_ptr<double> STDUPD; std::vector<STDUPD> vIn; vIn.push_back(STDUPD(new double(5))); vIn.push_back(STDUPD(new double(2))); vIn.push_back(STDUPD(new double(6))); using namespace std::placeholders; std::sort( vIn.begin(), vIn.end(), std::bind( std::less<double>(), std::bind(&STDUPD::operator*, _1), std::bind(&STDUPD::operator*, _2) ) ); std::vector<STDUPD>::const_iterator it = vIn.begin(), end = vIn.end(); for ( ; it != end; ++it) std::cout << ',' << **it; }
Another reason to avoid raw pointers if you can ...