我不能使用 C++11。
通常在将 std::for_each 与成员函数一起使用时,提供的数据结构会给出调用绑定成员函数的对象。但是,我想知道是否可以提供我自己的在整个循环中保持不变的对象,并将数据结构用于该成员函数的参数。
这是我想做的一个例子。
std::string GenerateKey(const std::vector<std::string>& parameters)
{
std::string key;
key.reserve(std::accumulate(parameters.begin(), parameters.end(), 0, std::mem_fun_ref(&std::string::length)));
std::for_each(parameters.begin(), parameters.end(), std::bind1st(std::mem_fun_ref(&std::string::append), key));
return key;
}
目的是对于参数中的每个字符串,append 将在键上调用,并从参数中传递一个字符串。
为什么这个 std::for_each 不起作用,如何或可以使它起作用?
我知道有其他方法可以连接字符串。我想更好地理解 C++ 函数式编程。
谢谢。
这是来自 VS2013 的错误消息,我怀疑它会有所帮助..
generator.cpp(28): error C2784: 'std::const_mem_fun1_ref_t<_Result,_Ty,_Arg> std::mem_fun_ref(_Result (__thiscall _Ty::* )(_Arg) const)' : could not deduce template argument for 'overloaded function type' from 'overloaded function type'
c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(910) : see declaration of 'std::mem_fun_ref'
c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(910) : see declaration of 'std::mem_fun_ref'
c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(910) : see declaration of 'std::mem_fun_ref'
c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(910) : see declaration of 'std::mem_fun_ref'
c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(910) : see declaration of 'std::mem_fun_ref'
c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(910) : see declaration of 'std::mem_fun_ref'
c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(910) : see declaration of 'std::mem_fun_ref'
c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(910) : see declaration of 'std::mem_fun_ref'
generator.cpp(28): error C2783: 'std::const_mem_fun1_ref_t<_Result,_Ty,_Arg> std::mem_fun_ref(_Result (__thiscall _Ty::* )(_Arg) const)' : could not deduce template argument for '_Result'
c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(910) : see declaration of 'std::mem_fun_ref'
generator.cpp(28): error C2783: 'std::const_mem_fun1_ref_t<_Result,_Ty,_Arg> std::mem_fun_ref(_Result (__thiscall _Ty::* )(_Arg) const)' : could not deduce template argument for '_Ty'
c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(910) : see declaration of 'std::mem_fun_ref'
generator.cpp(28): error C2783: 'std::const_mem_fun1_ref_t<_Result,_Ty,_Arg> std::mem_fun_ref(_Result (__thiscall _Ty::* )(_Arg) const)' : could not deduce template argument for '_Arg'
c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(910) : see declaration of 'std::mem_fun_ref'
generator.cpp(28): error C2784: 'std::const_mem_fun_ref_t<_Result,_Ty> std::mem_fun_ref(_Result (__thiscall _Ty::* )(void) const)' : could not deduce template argument for 'overloaded function type' from 'overloaded function type'
c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(901) : see declaration of 'std::mem_fun_ref'
c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(901) : see declaration of 'std::mem_fun_ref'
c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(901) : see declaration of 'std::mem_fun_ref'
c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(901) : see declaration of 'std::mem_fun_ref'
c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(901) : see declaration of 'std::mem_fun_ref'
c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(901) : see declaration of 'std::mem_fun_ref'
c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(901) : see declaration of 'std::mem_fun_ref'
c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(901) : see declaration of 'std::mem_fun_ref'
generator.cpp(28): error C2783: 'std::const_mem_fun_ref_t<_Result,_Ty> std::mem_fun_ref(_Result (__thiscall _Ty::* )(void) const)' : could not deduce template argument for '_Result'
c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(901) : see declaration of 'std::mem_fun_ref'
generator.cpp(28): error C2783: 'std::const_mem_fun_ref_t<_Result,_Ty> std::mem_fun_ref(_Result (__thiscall _Ty::* )(void) const)' : could not deduce template argument for '_Ty'
c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(901) : see declaration of 'std::mem_fun_ref'
generator.cpp(28): error C2784: 'std::mem_fun1_ref_t<_Result,_Ty,_Arg> std::mem_fun_ref(_Result (__thiscall _Ty::* )(_Arg))' : could not deduce template argument for 'overloaded function type' from 'overloaded function type'
c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(893) : see declaration of 'std::mem_fun_ref'
c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(893) : see declaration of 'std::mem_fun_ref'
c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(893) : see declaration of 'std::mem_fun_ref'
c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(893) : see declaration of 'std::mem_fun_ref'
c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(893) : see declaration of 'std::mem_fun_ref'
generator.cpp(28): error C2914: 'std::mem_fun_ref' : cannot deduce template argument as function argument is ambiguous
generator.cpp(28): error C2783: 'std::mem_fun1_ref_t<_Result,_Ty,_Arg> std::mem_fun_ref(_Result (__thiscall _Ty::* )(_Arg))' : could not deduce template argument for '_Result'
c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(893) : see declaration of 'std::mem_fun_ref'
generator.cpp(28): error C2783: 'std::mem_fun1_ref_t<_Result,_Ty,_Arg> std::mem_fun_ref(_Result (__thiscall _Ty::* )(_Arg))' : could not deduce template argument for '_Ty'
c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(893) : see declaration of 'std::mem_fun_ref'
generator.cpp(28): error C2783: 'std::mem_fun1_ref_t<_Result,_Ty,_Arg> std::mem_fun_ref(_Result (__thiscall _Ty::* )(_Arg))' : could not deduce template argument for '_Arg'
c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(893) : see declaration of 'std::mem_fun_ref'
generator.cpp(28): error C2784: 'std::mem_fun_ref_t<_Result,_Ty> std::mem_fun_ref(_Result (__thiscall _Ty::* )(void))' : could not deduce template argument for 'overloaded function type' from 'overloaded function type'
c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(884) : see declaration of 'std::mem_fun_ref'
c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(884) : see declaration of 'std::mem_fun_ref'
c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(884) : see declaration of 'std::mem_fun_ref'
c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(884) : see declaration of 'std::mem_fun_ref'
c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(884) : see declaration of 'std::mem_fun_ref'
c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(884) : see declaration of 'std::mem_fun_ref'
c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(884) : see declaration of 'std::mem_fun_ref'
c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(884) : see declaration of 'std::mem_fun_ref'
generator.cpp(28): error C2783: 'std::mem_fun_ref_t<_Result,_Ty> std::mem_fun_ref(_Result (__thiscall _Ty::* )(void))' : could not deduce template argument for '_Result'
c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(884) : see declaration of 'std::mem_fun_ref'
generator.cpp(28): error C2783: 'std::mem_fun_ref_t<_Result,_Ty> std::mem_fun_ref(_Result (__thiscall _Ty::* )(void))' : could not deduce template argument for '_Ty'
c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(884) : see declaration of 'std::mem_fun_ref'
generator.cpp(28): error C2780: '_Fn1 std::for_each(_InIt,_InIt,_Fn1)' : expects 3 arguments - 2 provided
c:\program files\microsoft visual studio 12.0\vc\include\algorithm(29) : see declaration of 'std::for_each'