Это был один из вопросов, обнаруженных на моем итоговом экзамене.Я не могу понять, что я должен делать.Я знаю, что BindSecArg требует оператор (), но не уверен, что происходит внутри.
В этом вопросе вы должны реализовать нечто подобное std :: bind2nd.Для простоты main пишется с использованием цикла for, но может быть переписан с использованием контейнеров «для каждого» и 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 решает эту проблему?