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
greater<double>::operator() is an inline function, so compilers inline-expand it during instantiation of sort.
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
The cause of the problem is that this particular STL platform has a bug in its handing of const member functions.
III
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.
Every compiler should accept this revised code.
iterator_traits
如果只对 T* 进行偏特化,iterator_traits<const int*>::value_type的类型就是const int。这一般不会是我们想要的,所以必须对 const T* 也进行特化。
为什么使用iterator_traits::value_type,而不是直接使用iter::value_type
作用: 兼容内置类型的指针
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?
作用: 兼容内置类型的指针
相关文章推荐
- How to create functions that can accept variable number of parameters such as Format
- warning C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead.
- Item42 Consider emplacement instead of insertion
- 错误:'fopen': This function or variable may be unsafe. Consider using fopen_s instead……
- Effective Java 78 Consider serialization proxies instead of serialized instances
- 错误 1 error C4996: 'scanf': This function or variable may be unsafe. Consider using scanf_s instead.
- This function or variable may be unsafe Consider using xxx instead
- Return objects to enable chaining of functions
- Scala-5 - 1 - Lecture 4.1 - Functions as Objects (8_04)
- error C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead.
- Illustrating the generic search algorithm: Search for first occurrence of deque's contents as a subs
- Android Studio: Error:Cannot locate factory for objects of type DefaultGradleConnector, as ConnectorServiceRegistry
- 'localtime': This function or variable may be unsafe. Consider using localtime_s instead.
- 'localtime': This function or variable may be unsafe. Consider using localtime_s instead. To disable
- 实习第16天 开新坑 Effective Java 英文 第二版 读书笔记 Item 1: Consider static factory methods instead of constructors
- warning C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. T
- error C4996: 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To di
- Error:Cannot locate factory for objects of type DefaultGradleConnector, as ConnectorServiceRegistry
- With form validation: why onSubmit=“return functionname()” instead of onSubmit=“functionname()”?
- Effective Java - Item 1: Consider static factory methods instead of constructors