C++ STL:std::bind1st用于std::一元函数
AFAIKC++ STL:std::bind1st用于std::一元函数,c++,stl,functional-programming,bind,C++,Stl,Functional Programming,Bind,AFAIKstd::bind1st接受一个二进制函子和一个参数,并返回一个一元函子,其中第一个参数已经绑定。STL是否提供了一个类似于std::bind1st的函数,它接受一元函数和一个参数,并返回一个没有参数的函数 编辑:对于比C++11更旧的版本,我需要一个解决方案(我没有放那个标签)一种方法是自己编写一个活页夹 template<typename FUNCTION, typename ARG_TYPE, typename RETURN_TYPE> struct bind_it
std::bind1st
接受一个二进制函子和一个参数,并返回一个一元函子,其中第一个参数已经绑定。STL是否提供了一个类似于std::bind1st的函数,它接受一元函数和一个参数,并返回一个没有参数的函数
编辑:对于比C++11更旧的版本,我需要一个解决方案(我没有放那个标签)一种方法是自己编写一个活页夹
template<typename FUNCTION, typename ARG_TYPE, typename RETURN_TYPE>
struct bind_it
{
FUNCTION function;
ARG_TYPE argument;
bind_it(ARG_TYPE value, FUNCTION f):function(f){
argument = value;
}
RETURN_TYPE operator()(){
return function(argument);
}
};
模板
结构绑定
{
功能;
ARG_型参数;
绑定它(参数类型值,函数f):函数(f){
参数=值;
}
返回类型运算符(){
返回函数(参数);
}
};
我相信有更好的方法写活页夹
像这样使用它
int f(int i){
return i + 2;
}
int main()
{
bind_it<int(*)(int), int, int> bound(5, f);
int se7en = bound();
}
intf(inti){
返回i+2;
}
int main()
{
绑定(5,f);
int se7en=bound();
}
看来STL没有提供解决方案(std::bind
来自C++11),所以我的版本是:
#include <iostream>
#include <functional>
template <typename Operation>
class bound_unary_function
: public std::unary_function<typename Operation::argument_type,
typename Operation::result_type> {
typedef bound_unary_function<Operation> _Self;
Operation op;
typename _Self::argument_type arg;
public:
bound_unary_function(const Operation& _op, const typename _Self::argument_type& _arg)
: op(_op), arg(_arg) { }
typename _Self::result_type operator()() {
return op(arg);
}
};
template <typename Operation>
bound_unary_function<Operation> bind_unary_function(const Operation& op,
const typename Operation::argument_type& arg) {
return bound_unary_function<Operation>(op, arg);
}
int inc(int x) {
return ++x;
}
int main() {
std::cout << "inc(0)=" << bind_unary_function(std::ptr_fun(inc), 0)() << '\n';
return 0;
}
#包括
#包括
样板
类界一元函数
:公共标准::一元函数{
typedef绑定一元函数Self;
手术操作;
类型名称_Self::参数_typearg;
公众:
绑定的一元函数(常量运算和常量运算,常量类型名称和参数类型和参数)
:op(_op),arg(_arg){}
typename _Self::result_type运算符()(){
返回op(arg);
}
};
样板
绑定一元函数绑定一元函数(常量运算和运算,
常量typename操作::参数(类型和参数){
返回界一元函数(op,arg);
}
int公司(INTX){
return++x;
}
int main(){
STD::CUT作为C++ 11,<代码> STD::BooN/COD>一般工作,lambdas也如此。<代码> Boo::BooN/COD>?Boost?lambda /Booost?凤凰?@ @ CATAGIN IRAFE C++03@chris我需要STL解决方案