这个C++是什么意思?结构?

发布于 2024-09-14 07:37:23 字数 246 浏览 5 评论 0原文

我得到一个简单的 C++ 结构,如下所示:

// Functor for peak to decreasing intensity sorting
struct cmp_decr_int2
{
    bool operator() (peak2 a, peak2 b) 
    { 
        return a.int2 > b.int2;
    }   
};

此示例中是否存在运算符重载?

I got a simple C++ struct as follows:

// Functor for peak to decreasing intensity sorting
struct cmp_decr_int2
{
    bool operator() (peak2 a, peak2 b) 
    { 
        return a.int2 > b.int2;
    }   
};

is there an overload of the operator in this sample?

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

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

发布评论

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

评论(5

黯淡〆 2024-09-21 07:37:23

是的。 operator() 被称为“函数调用”运算符,它允许对象像函数一样使用。这样的类称为“函子”。

一种常见的模式是创建比较两个事物的相等性或关系的函子,以用于需要比较谓词的任何事物。 (例如,这个可以在 std::map 中使用。它会有一个像 cmp_decr_int2compare; 这样的成员,然后它可以比较两个事物之间的关系: if (compare(x, y)) /* x is less than y, by some metric */)

这个特定的结构体通过比较两个 peak2 的 <代码>int2成员。可以更好地写为:

struct cmp_decr_int2
{
    //                     note const! vvvvv
    bool operator() (peak2 a, peak2 b) const
    { 
        return a.int2 > b.int2;
    }   
};

该函数应该是 const,因为它不需要更改任何成员(没有需要更改的成员。)const-正确性很重要。*

在许多情况下,这些函子在参数所在的上下文中使用它们本身是 const,因此您应该按照示例中的值获取参数,或者通过常量引用获取参数。

您应该更喜欢通过 const 引用而不是按值传递类型,除非该类型是基本类型(float、unsigned int、double 等)或小于 void*。在大多数情况下,您将通过 const-reference 传递:

struct cmp_decr_int2
{
    // note const&:  vvvvv       v    vvvvv       v    vvvvv
    bool operator() (const peak2 & a, const peak2 & b) const
    { 
        return a.int2 > b.int2;
    }   
};


*例如,如果将其用作 std::map 中的谓词,则在没有 const 的情况下,映射将无法在 内比较两个事物>const 函数。

Yes. operator() is called the "function call" operator, and allows an object to be usable as if it were a function. Such a class is called a "functor".

A common pattern is to make functors that compare two things for equality or relations, for use in anything requiring a comparison predicate. (This one could be usable in an std::map, for example. It would have a member likecmp_decr_int2 compare; and then it could compare the relation between two things with: if (compare(x, y)) /* x is less than y, by some metric */)

This particular struct orders two peak2's by comparing their int2 members. It could be better written as:

struct cmp_decr_int2
{
    //                     note const! vvvvv
    bool operator() (peak2 a, peak2 b) const
    { 
        return a.int2 > b.int2;
    }   
};

The function should be const because it does not need to change any members (there are none to change.) const-correctness is important.*

In many cases these functors are used in contexts where the arguments themselves are const, so you should either take the arguments by value as in the example or by constant reference.

You should prefer to pass types by const-reference over by-value, except when that type is fundamental (float, unsigned int, double, etc.) or smaller than a void*. In most cases, then, you will pass by const-reference:

struct cmp_decr_int2
{
    // note const&:  vvvvv       v    vvvvv       v    vvvvv
    bool operator() (const peak2 & a, const peak2 & b) const
    { 
        return a.int2 > b.int2;
    }   
};


*If this were used as a predicate in a std::map, for example, without const the map wouldn't be able to compare two things while within a const function.

瀟灑尐姊 2024-09-21 07:37:23

C++ 中的结构只是具有默认访问器 public 而不是 private 的类。所以是的,这会导致函数重载。

Structs in C++ are just classes with a default accessor of public instead of private. So yes, that would have a function overload.

娜些时光,永不杰束 2024-09-21 07:37:23

在 C++ 中,结构在各方面都类似于类,只是默认参数访问是 public: 而不是 private:。当定义类型的使用范围非常狭窄时(例如在简单仿函数的示例中),通常的做法是使用 struct 而不是 class。

此示例的作用是模拟函数指针的外观,而不存在可能为 null 的脆弱性。

这里的operator()成员重载了函数调用。当您尝试执行以下操作时:

cmp_decr_int2 foo;
foo(peek2(), peek2());

该重载成员被调用。

in c++ a struct is in every way like a class, except that the default parameter access is public: rather than private:. It is a common practice to use struct instead of class when the scope of the defined type's use is very narrow, such as in the example of a simple functor.

What this example does is emulate the appearance of a function pointer without the fragility of possibly being null.

The operator() member here overloads function calling. when you try to do something like:

cmp_decr_int2 foo;
foo(peek2(), peek2());

that overload member gets called.

鯉魚旗 2024-09-21 07:37:23
#include <iostream>
using namespace std;

struct cmp_decr_int2
{
    bool operator() (int a, int b)
    { 
        return a > b;
    }

    bool operator() (int i)
    {
        return i > 0;
    }
};

int main()
{
    cmp_decr_int2 a;
    cout << a(1, 2) << endl;
    cout << a(1) << endl;
}

是的,肯定可以重载该函数!这对我来说非常有效。

#include <iostream>
using namespace std;

struct cmp_decr_int2
{
    bool operator() (int a, int b)
    { 
        return a > b;
    }

    bool operator() (int i)
    {
        return i > 0;
    }
};

int main()
{
    cmp_decr_int2 a;
    cout << a(1, 2) << endl;
    cout << a(1) << endl;
}

Yep, sure can overload the function! This worked perfectly for me.

对风讲故事 2024-09-21 07:37:23

$13.5.4 状态

-operator() 应是具有任意数量参数的非静态成员函数。它可以有默认参数。它实现函数调用语法 postfix-expression ( expression-listopt ),其中 postfix-expression 求值为类对象,并且可能为空的表达式列表与该类的operator() 成员函数的参数列表相匹配。因此,如果 T::operator()(T1, T2, T3) 存在,则对于 T 类型的类对象 x,调用 x(arg1,...) 被解释为 x.operator()(arg1, ...)以及重载决策机制是否将运算符选择为最佳匹配函数(13.3.3)。

因此,结构体“cmp_decr_int2”肯定重载了operator()。

请注意,这也是 C++ 中唯一可以接受可变数量参数的运算符。

$13.5.4 states-

operator() shall be a non-static member function with an arbitrary number of parameters. It can have default arguments. It implements the function call syntax postfix-expression ( expression-listopt ) where the postfix-expression evaluates to a class object and the possibly empty expression-list matches the parameter list of an operator() member function of the class. Thus, a call x(arg1,...) is interpreted as x.operator()(arg1, ...) for a class object x of type T if T::operator()(T1, T2, T3) exists and if the operator is selected as the best match function by the overload resolution mechanism (13.3.3).

Therefore, the struct 'cmp_decr_int2' has definitely overloaded operator().

Note that this is also the only operator in C++ that can take variable number of arguments.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文