Effective STL 46 Consider function objects instead of functions as algorithm parameters

Function objects as parameters to algorithms thus offer more than greater efficiency. They’re also more robust when it comes to getting your code to compile.
I. the version using greator<double> was faster every time

vector<double> v;
...
sort(v.begine(), b.end(), greater<double>());
inline
bool doubleGreater(double d1, double d2) {
    return d1 > d2;
}

greater<double>::operator() is an inline function, so compilers inline-expand it during instantiation of sort.

void sort(vector<double>::iterator first, vector<double>::iterator last;
          bool(*comp)(double, double));

because comp is a pointer to a function, each time it’s used inside sort, compilers make an indirect function call - a call through a pointer. Most compilers won’t try to inline calls to functions that are invoked through function pointer even if such functions have been declared inline and the optimization appears to be straightforward.
II

set<string> s;
...
transform(s.begin(), s.end(), ostream_iterator<string::size_type>(cout, "\n"), mem_fun_ref(&string::size));

The cause of the problem is that this particular STL platform has a bug in its handing of const member functions.

struct StringSize:
    public unary_function<string, string::size_type> {
    string::size_type operator()(const string& s) const {
        return s.size();
    }
};
transform(s.begin(), s.end(), ostream_iterator<string::size_type>(cout, "\n"), StringSize());

III

template<typename FPType>
FPType average(FPType val1, FPType val2) {
    return (val + val) / 2;
}

template<typename inputiter1, typename inputiter2>
void writeAverage(inputiter1 begin1, inputiter1 end1, inputiter2 begin2, ostream&s) {
    transform(begin1, end1, begin2, ostream_iterator<typename iterator_traits<inputiter1>::value_type(s, "\n"), 
    average<typename iterator_traits<inputiter1>::value_type>); // error!
}

Many compilers accept this code, but the C++ Standard appears to forbid it. If there were an another function template named average that takes a single type parameter, the expression average<typename iterator_traits<InputIter1>::value_type> would be ambiguous, because it would not be clear which template to instantiate.

template<typename FPType>
struct Average:
    public binary_function<FPType, FPType, FPType> {
        FPType operator()(FPType val1, FPType val2) const {
            return average(val1, val2);
        }
};

template<typename inputiter1, typename inputiter2>
void writeAverage(inputiter1 begin1, inputiter1 end1, inputiter2 begin2, ostream&s) {
    transform(begin1, end1, begin2, 
    ostream_iterator<typename iterator_traits<inputiter1>::value_type(s, "\n"),
    Average<typename iterator_traits<inputiter1>::value_type>());
}

Every compiler should accept this revised code.

iterator_traits

template<class Iterator>  
struct iterator_traits  
{  
    typedef typename Iterator::category iterator_category;  
    typedef typename Iterator::value_type value_type;  
    typedef typename Iterator::difference_type difference_type;  
    typedef typename Iterator::pointer pointer;  
    typedef typename Iterator::reference reference;  
};  

template<class T>  
struct iterator_traits<T*>  
{  
    typedef random_access_iterator_tag iterator_category;  
    typedef T value_type;  
    typedef ptrdiff_t difference_type;  
    typedef T* pointer;  
    typedef T & reference;  
};  

template<class T>  
struct iterator_traits<const T*>  
{  
    typedef random_access_iterator_tag iterator_category;  
    typedef T value_type;  
    typedef ptrdiff_t difference_type;  
    typedef const T* pointer;  
    typedef const T & reference;  

};  

如果只对 T* 进行偏特化,iterator_traits<const int*>::value_type的类型就是const int。这一般不会是我们想要的,所以必须对 const T* 也进行特化。

为什么使用iterator_traits::value_type,而不是直接使用iter::value_type

typedef vector<int>::iterator intVecIter;
typedef intVecIter::value_type Int_type;     //如果 vector<int> 采用 int* 作为其 iterator
                                             //这就不可能有效 (int*)::value_type?

作用: 兼容内置类型的指针

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值