c++ - 如何将 std::for_each 与成员函数一起使用,其中提供的数据结构给出参数而不是对象

标签 c++

我不能使用 C++11。

通常在将 std::for_each 与成员函数一起使用时,提供的数据结构会给出调用绑定(bind)成员函数的对象。但是,我想知道是否可以提供我自己的在整个循环中保持不变的对象,并使用数据结构作为此成员函数的参数。

这是我想做的一个例子。

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'

最佳答案

std::string::append 已重载,编译器无法知道您指的是哪个函数。

所以,std::string::append 实际上是指一组函数。

关于c++ - 如何将 std::for_each 与成员函数一起使用,其中提供的数据结构给出参数而不是对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18252530/

相关文章:

c++ - 在opencv中绘制四边形

C# WinForms 程序,能够为其制作 C++ 插件

c++ - 关于类模板部分特化和类成员特化的一些混淆

c++ - 将 std::string 转换为 char* 的争论

c++ - random_ints(a,N) 有什么作用以及如何在我的代码中使用它?

c++ - 当 CRT 动态链接时,重载的全局运算符 new(和变体)如何工作

c++ - 正在编译具有相同迭代器的嵌套循环,这是为什么?

c++ - 在 QDialog::exec() 之后调用 QApplication::exec()

c++ - 使用 gcc 强制实例化对象

c++ - 全局变量和链接器错误