C++绑定第二个问题
这是我期末考试中出现的问题之一。我不知道我应该做什么。 我知道 BindSecArg 需要一个 () 运算符,但不确定里面的内容。
在这个问题中,您需要实现类似于 std::bind2nd 的东西。为了简单起见主要 使用“for”循环编写,但也可以用“foreach”和STL容器重写。
class Functor1 {
public:
int operator()(const int & i, const int & j) const {
return i+j;
}
};
class Functor2 {
public:
int operator()(const int & i, const int & j) const {
return i*j;
}
};
template <typename T>
class BindSecArg
};
int main () {
Functor1 f1;
for (int i=0; i<10; ++i) std::cout << f1(i,i) << " "; //0 2 4 6 8 10
std::cout << std::endl;
Functor2 f2;
for (int i=0; i<10; ++i) std::cout << f2(i,i) << " "; //0 1 4 9 16 25
std::cout << std::endl;
BindSecArg<Functor1> b1(4); //bind second argument of Functor1 to 4
for (int i=0; i<10; ++i) std::cout << b1(i) << " "; //4 5 6 7 8 9
std::cout << std::endl;
BindSecArg<Functor2> b2(4); //bind second argument of Functor2 to 4
for (int i=0; i<10; ++i) std::cout << b2(i) << " "; //0 4 8 12 16 20
std::cout << std::endl;
}
额外加分问题:您的实现很可能不起作用(这没关系!)
class Functor3 {
public:
std::string operator()(const std::string & i, const std::string & j) const {
return i+j;
}
};
STL 如何解决这个问题?
This was one of the questions showed up on my Final exam. I can't figure out what I'm supposed to do.
I know BindSecArg requires a () operator, but not sure what goes inside.
In this question you are required to implement something similar to std::bind2nd. For simplicity main
is written using a ”for”-loop, but it may be rewritten with ”for each” and STL containers.
class Functor1 {
public:
int operator()(const int & i, const int & j) const {
return i+j;
}
};
class Functor2 {
public:
int operator()(const int & i, const int & j) const {
return i*j;
}
};
template <typename T>
class BindSecArg
};
int main () {
Functor1 f1;
for (int i=0; i<10; ++i) std::cout << f1(i,i) << " "; //0 2 4 6 8 10
std::cout << std::endl;
Functor2 f2;
for (int i=0; i<10; ++i) std::cout << f2(i,i) << " "; //0 1 4 9 16 25
std::cout << std::endl;
BindSecArg<Functor1> b1(4); //bind second argument of Functor1 to 4
for (int i=0; i<10; ++i) std::cout << b1(i) << " "; //4 5 6 7 8 9
std::cout << std::endl;
BindSecArg<Functor2> b2(4); //bind second argument of Functor2 to 4
for (int i=0; i<10; ++i) std::cout << b2(i) << " "; //0 4 8 12 16 20
std::cout << std::endl;
}
Extra credit question: your implementation most probably doesn’t work (which is OK!) with
class Functor3 {
public:
std::string operator()(const std::string & i, const std::string & j) const {
return i+j;
}
};
how does STL solve this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
BindSecArg
的operator()
需要接受一个参数(显然),它应该做的是从“bound”函子,向其传递 (a) 传入的“第一个”参数和 (b) “bound”第二个参数。因此,我们需要构造绑定函子类的实例(以便我们可以进行该调用),并且需要记住第二个参数。我们将通过数据成员来处理这两个问题。
看起来像:
标准库(请不要说“STL”)
bind2nd
通过期望 T 是“AdaptableBinaryFunction”来解决这个问题,即提供一些typedef
成员识别operator()
的参数和结果类型,然后使用它们从基类继承,使用这些 typedef 作为模板类型,然后使用基类提供的 typedef 来模板化它自己的类型operator()
实现。这些是“模板元编程”的一些基本技术,它很快就会变得复杂。您应该为此查找一些单独的阅读资源。The
operator()
forBindSecArg
needs to take one argument (obviously), and what it's supposed to do is call theoperator()
from the "bound" functor, passing it (a) the passed-in "first" argument and (b) the "bound" second argument.So we need to construct an instance of the bound functor's class (so that we can make that call), and we need to remember the second argument. We'll take care of both of these with data members.
That looks like:
The standard library (please don't say "STL")
bind2nd
addresses this by expecting T to be an "AdaptableBinaryFunction", i.e. to provide sometypedef
members identifying the parameter and result types foroperator()
, and then using these to inherit from a base class using those typedefs as template types, and then using typedefs provided by the base class to template its ownoperator()
implementation. These are some of the basic techniques of "template metaprogramming", and it gets complicated fast. You should look up some separate reading resources for this.可能有更好的实现:
int我在你的问题的评论中发布的链接你可以找到stl代码。
probably there are better implementations:
int the link I posted in the comment to your question you can find the stl code.
内部调用 Functor.operator(),将其构造函数中的 BindSecArg 值作为第二个参数传递。
Inside goes a call to Functor.operator(), passing the value given to
BindSecArg
in its constructor as the second argument.