C++编写一个modify_if函数

发布于 2024-08-06 15:50:36 字数 382 浏览 1 评论 0原文

我有一个硬件问题,这让我对我必须做什么感到困惑。问题如下:

  1. 这个想法是设计一个名为Modify_If的通用函数,它将接受输入x(通过引用传递)和两个函子f1和f2。函数Modify_If将使用函子f1来确定x是否满足某个条件。如果是这样,Modify_if 将通过应用函子 f2 来更改 x 的值。

修改_If 的原型如下:

template <class C, class Tester, class Transform>
void Modify_If(C & a, Tester f, Transform g)

我必须编写修改_If 函数,但我不知道从哪里开始,所以如果有人可以帮助我,我将不胜感激。

I have a hw question which confuses me on what i have to do. The question is below:

  1. The idea is to design a generic function called Modify_If that will take an input x (passed by reference), and two functors f1 and f2. The function Modify_If will use functor f1 to determine whether x obeys a certain condition. If it does, Modify_if will change the value of x, by applying functor f2 to it.

The prototype for Modify_If is as follows:

template <class C, class Tester, class Transform>
void Modify_If(C & a, Tester f, Transform g)

I have to write the Modify_If function but I have no idea where to start so if anyone can help me out I would appreciate it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

宫墨修音 2024-08-13 15:50:36
template <class C, class Tester, class Transform>
void Modify_If(C& a, Tester f1, Transform f2) {
    if (f1(a)) // Apply f1 to a - Check whether result is true
        a = f2(a); // Transform with f2; save
}
template <class C, class Tester, class Transform>
void Modify_If(C& a, Tester f1, Transform f2) {
    if (f1(a)) // Apply f1 to a - Check whether result is true
        a = f2(a); // Transform with f2; save
}
初心 2024-08-13 15:50:36

从描述来看,听起来您应该做的就是在 f(a) 返回 true 时执行 g(a) 。那看起来像这样:

if(f(a)) {
  g(a);
}

From the description it sounds like all you're supposed to do is to execute g(a) if f(a) returns true. That would look like this:

if(f(a)) {
  g(a);
}
紫轩蝶泪 2024-08-13 15:50:36

函数原型可能如下所示:

template<class ForwardIterator, class Predicate, class Type>
void transform_if(
      ForwardIterator First, 
      ForwardIterator Last,
      Predicate Pred, 
      UnaryFunction Func
   );

在其实现中,您应该迭代所有元素集,并将 Func 应用于将为 Pred(*element_iterator) 返回 true 的所有元素。

Function prototype could look as follows:

template<class ForwardIterator, class Predicate, class Type>
void transform_if(
      ForwardIterator First, 
      ForwardIterator Last,
      Predicate Pred, 
      UnaryFunction Func
   );

In its implementation you should iterate through all set of elements and apply Func to all elements that will return true for Pred(*element_iterator).

坐在坟头思考人生 2024-08-13 15:50:36

在函子 TesterTransform 中,您所要做的就是为每个函子创建一个类,并重载 () 运算符。然后像函数一样编写函子的主体。这是一个简单的函子骨架:

class Functor
{
public:
   template <class Type>
   void operator()(Type& value) // it doesn't have to be void.
   {
     // functor body, as if it were a function.
   }
};

然后,您可以通过创建函子的对象或轻松地动态创建它来将函子作为参数传递:

template <class Func>
void fun(Func f); // a function hat accepts a functor.
Functor f;
fun(f);
fun(Functor()); // creating the functor on the fly.

In both functors Tester and Transform, all what you have to do is to create a class for each, and overload the () operator. Then write the body of the functor as if it were a function. This is a simple functor skeleton:

class Functor
{
public:
   template <class Type>
   void operator()(Type& value) // it doesn't have to be void.
   {
     // functor body, as if it were a function.
   }
};

Then, you can pass the functor as a parameter, by creating an object of it, or easily by creating it on the fly:

template <class Func>
void fun(Func f); // a function hat accepts a functor.
Functor f;
fun(f);
fun(Functor()); // creating the functor on the fly.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文