C++我自己的类谓词不起作用

发布于 2025-01-11 18:55:15 字数 1298 浏览 0 评论 0原文

我是 C++ 的新手。我想创建我自己的谓词。但布尔运算符的部分似乎是错误的(至少在我看来是错误的)。有人可以给我提示吗?我不想改变这个想法的整体结构,我只是确定我不理解有关operator()实现的一些细节或与c++中的类相关的东西。

#include <iostream>
#include <vector>

class Predicate
{

private:
    int number = 0;

public:
    Predicate() = default;
    Predicate(const int number)
    {
        this->number = number;
    }
    bool operator()(int value) const
    {
        Predicate *pred = new Predicate();
        bool result = pred->operator()(value);
        return result;
    }
};

class Even : public Predicate
{
    bool operator()(int value) const
    {
        return value % 2 == 0;
    }
};

class Negative : public Predicate
{
    bool operator()(int value) const
    {
        return value < 0;
    }
};

int count(const std::vector<int> &elements, const Predicate &predicate)
{
    int count = 0;
    for (int index = 0; index < elements.size(); ++index)
    {
        if (predicate(elements[index]))
        {
            ++count;
        }
    }
    return count;
}

int main()
{
    const std::vector<int> elements{-7, 12, -11, 2, 9, -4, -6, 5, 23, -1};
    std::cout << count(elements, Even()) << " " << count(elements, Negative()) << std::endl;
}

I'm a complete newbie at C++. I want to create my own predicate. But the part with bool operator seems to be wrong (at least in my humble opinion). Could someone give me a hint? I don't want to change the overall structure of this idea, I'm just sure I don't understand some details about operator () implementation or something related to classes in c++.

#include <iostream>
#include <vector>

class Predicate
{

private:
    int number = 0;

public:
    Predicate() = default;
    Predicate(const int number)
    {
        this->number = number;
    }
    bool operator()(int value) const
    {
        Predicate *pred = new Predicate();
        bool result = pred->operator()(value);
        return result;
    }
};

class Even : public Predicate
{
    bool operator()(int value) const
    {
        return value % 2 == 0;
    }
};

class Negative : public Predicate
{
    bool operator()(int value) const
    {
        return value < 0;
    }
};

int count(const std::vector<int> &elements, const Predicate &predicate)
{
    int count = 0;
    for (int index = 0; index < elements.size(); ++index)
    {
        if (predicate(elements[index]))
        {
            ++count;
        }
    }
    return count;
}

int main()
{
    const std::vector<int> elements{-7, 12, -11, 2, 9, -4, -6, 5, 23, -1};
    std::cout << count(elements, Even()) << " " << count(elements, Negative()) << std::endl;
}

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

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

发布评论

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

评论(1

月亮邮递员 2025-01-18 18:55:15

您需要的是:

  • 将 Predicate 定义为抽象类型,
  • 并实现它的不同版本。

Predicate 作为抽象类型:

class Predicate {
  public:
    virtual bool operator(int v) const = 0;
};

实现(实现)给定的 Predicate

class IsNegative : public Predicate { // means IsNegatives are Predicates
  public:
    virtual bool operator(int v) const { return v<0; } // realisation of the operator
};

What you need is:

  • define Predicate as an abstract type,
  • implements different versions of it.

Predicate as an abstract type:

class Predicate {
  public:
    virtual bool operator(int v) const = 0;
};

Implementing (realising) a given Predicate:

class IsNegative : public Predicate { // means IsNegatives are Predicates
  public:
    virtual bool operator(int v) const { return v<0; } // realisation of the operator
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文