STL的Binder2nd中的疑问

shmiloveyou 2013-05-27 07:19:02
一个for_each结合bind2nd测试引发的疑问:

#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;

void printElem( int elem, const char* prefix )
{
cout << prefix << elem <<endl;
}

int main( void )
{
int ia[] = { 1, 2, 3 };
vector<int> ivec( ia, ia + sizeof(ia) / sizeof(int) );
for_each( ivec.begin(), ivec.end(), bind2nd( ptr_fun(printElem), "Element:" ) );

return 0;
}


摘自VS2010
//疑问1:这个unary_function中的_Fn2类型和binder2nd中的_Fn2中的类型一致吗?我的理解是前者是一元函数类型,而后者是二元函数类型,类型不一致啊

template<class _Fn2> //VS2010中的
class binder2nd : public unary_function<typename _Fn2::first_argument_type, typename _Fn2::result_type>
{ // functor adapter _Func(left, stored)
public:
typedef unary_function<typename _Fn2::first_argument_type, typename _Fn2::result_type> _Base;
typedef typename _Base::argument_type argument_type;
typedef typename _Base::result_type result_type;

binder2nd(const _Fn2& _Func, typename _Fn2::second_argument_type& _Right) : op(_Func), value(_Right)
{
// construct from functor and right operand
}

result_type operator()(const argument_type& _Left) const
{
// apply functor to operands
return (op(_Left, value));
}

result_type operator()(argument_type& _Left) const
{ // apply functor to operands
return (op(_Left, value));
}

protected:
_Fn2 op; // the functor to apply
typename _Fn2::second_argument_type value; // the right operand
};

疑问2:"_Fn2::"在此处是表示函数名加作用域标识符来访问数据类型,我不懂这种使用方法。能给我一些理解这种使用方法的资料或文档吗?

// TEMPLATE FUNCTION bind2nd
template<class _Fn2, class _Ty>
inline binder2nd<_Fn2> bind2nd(const _Fn2& _Func, const _Ty& _Right)
{ // return a binder2nd functor adapter
typename _Fn2::second_argument_type _Val(_Right);
return (_STD binder2nd<_Fn2>(_Func, _Val)); //返回一个binder2nd的实例
}


for_each函数的一种实现

template<typename InputIterator, typename Function>
Function for_each(InputIterator beg, InputIterator end, Function f)
{
while(beg != end)
f(*beg++);
//如果f是bind2nd的返回值(返回binger2nd的一个对象),则此处调用binder2nd类重载的result_type operator()(argument_type& _Left) const函数
}
//注:个人理解
f(*beg++);在这个测试环境下等价于
f(*beg++)
{
_Func( *beg++, _Right);
}

请高手帮我解决以上红色文字标记的两处疑问。如果能讲解下binder2nd这个类和bind2nd这个辅助函数更好。谢谢。
...全文
72 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
typeid输出一下类型
cout<<typeid( ptr_fun(printElem)).name()<<endl;
class std::pointer_to_binary_function<int,char const *,void,void (__cdecl*)(int,char const *)>
shmiloveyou 2013-05-28
  • 打赏
  • 举报
回复
引用 1 楼 akirya 的回复:
binder2nd之后的函数对象 就是1元类型了,只接受一个参数。
这个地方我能理解,不明白的就是

template<class _Fn2>   //VS2010中的
class binder2nd : public unary_function<typename _Fn2::first_argument_type, typename _Fn2::result_type>  
binder2nd(const _Fn2& _Func,  typename _Fn2::second_argument_type& _Right) : op(_Func), value(_Right)
中的_Fn2的类型问题。 就这个实例而言,此处_Fn2是什么类型?请指教。
  • 打赏
  • 举报
回复
binder2nd之后的函数对象 就是1元类型了,只接受一个参数。 _Fn2::first_argument_type 是使用_Fn2的内嵌类型first_argument_type 先看看专门讲stl的书籍吧,binder2nd 三言两语说不清楚的。
shmiloveyou 2013-05-28
  • 打赏
  • 举报
回复
找到了一本很适合新手看的STL入门书籍--《C++ Templates(简体中文扫描版).pdf》,深浅相宜。此书被称为C++ STL世界的圣经。分享给大家:http://download.csdn.net/detail/qq2399431200/5468663

13,825

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder相关内容讨论区
社区管理员
  • 基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧