我想学习如何使用绑定函数.这是一个想法:我有这个参数的功能:
void print_i(int t, std::string separator)
{
std::cout << t << separator;
}
Run Code Online (Sandbox Code Playgroud)
我想这样做:
std::vector<int> elements;
// ...
for_each(elements.begin(), elements.end(), std::bind2nd(print_i, '\n'));
Run Code Online (Sandbox Code Playgroud)
但它不起作用!
这是我得到的:
/usr/include/c++/4.3/backward/binders.h: In instantiation of ‘std::binder2nd<void ()(int, std::string)>’:
main.cpp:72: instantiated from here
/usr/include/c++/4.3/backward/binders.h:138: error: ‘void ()(int, std::string)’ is not a class, struct, or union type
/usr/include/c++/4.3/backward/binders.h:141: error: ‘void ()(int, std::string)’ is not a class, struct, or union type
/usr/include/c++/4.3/backward/binders.h:145: error: ‘void ()(int, std::string)’ is not a class, struct, or union type
/usr/include/c++/4.3/backward/binders.h:149: error: ‘void ()(int, std::string)’ is …Run Code Online (Sandbox Code Playgroud)