您的位置:首页 > 移动开发 > Objective-C

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

2017-10-16 15:40 393 查看
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
b1f9
<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?


作用: 兼容内置类型的指针
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐