帮助理解 C++、模板、operator() 的类示例代码

发布于 2024-09-10 16:59:09 字数 665 浏览 5 评论 0原文

我不确定下面的类到底是做什么的,我们有一个类示例。在下面的代码中,operator() 在这种情况下做了什么?我不太明白 *(begin + first) 和几乎整个返回表达式作为正在评估的内容。任何帮助都会很棒。谢谢!

// IndexCompare.h - interface for IndexCompare class template
#ifndef _INDEXCOMPARE_H_
#define _INDEXCOMPARE_H_
#pragma once

template <class random_iterator>
class IndexCompare {
public:
    IndexCompare(random_iterator begin, random_iterator end)
        : begin(begin), end(end) {}

    ~IndexCompare() {}

    bool operator() (unsigned int first, unsigned int second) {
            return (*(begin + first) < *(begin + second));

    }

private:
    random_iterator begin;
    random_iterator end;
};

#endif

I'm not sure exactly what the following class does that we have for a class example. In the following code, what does the operator() do in this case? I don't quite get the *(begin + first) and pretty much the whole return expression as what is being evaluated. Any help would be great. Thanks!

// IndexCompare.h - interface for IndexCompare class template
#ifndef _INDEXCOMPARE_H_
#define _INDEXCOMPARE_H_
#pragma once

template <class random_iterator>
class IndexCompare {
public:
    IndexCompare(random_iterator begin, random_iterator end)
        : begin(begin), end(end) {}

    ~IndexCompare() {}

    bool operator() (unsigned int first, unsigned int second) {
            return (*(begin + first) < *(begin + second));

    }

private:
    random_iterator begin;
    random_iterator end;
};

#endif

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

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

发布评论

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

评论(3

泪是无色的血 2024-09-17 16:59:09

如果您问operator() 的作用,它允许您像函数一样调用对象。有关示例,请参阅本文

如果您询问示例中的函数正在做什么,它会比较由传递给函数的索引指定的两个元素的值。 begin + first 指的是从迭代器 begin 开始的索引 first 处的元素,类似地 begin + secondary*(begin + first) 获取该位置的值。

您可以通过传入一对迭代器将此类与任何 STL 容器(支持随机访问)一起使用。例如,您可以将其与如下向量一起使用:

vector<int> vec;
/* add some elements here */
IndexCompare<vector<int>::iterator> compare(vec.begin(), vec.end());

例如,现在调用 compare(2, 5) 将比较 vec[2]的值vec[5]

If you're asking what operator () does, it allows you to call the object like a function. See this article for an example.

If you're asking what the function in your example is doing, it's comparing the values of two elements specified by the indices passed to the function. begin + first refers to the element at index first starting from the iterator begin, similarly begin + second. *(begin + first) gets the value at that location.

You can use this class with any STL container (that supports random access) by passing in a pair of iterators. For example, you could use it with a vector like this:

vector<int> vec;
/* add some elements here */
IndexCompare<vector<int>::iterator> compare(vec.begin(), vec.end());

Now calling compare(2, 5) for example would compare the values of vec[2] and vec[5].

滥情空心 2024-09-17 16:59:09

begin 是一个迭代器(认为是指针),因此 begin + first 前进到第一个位置。
或者在第二种情况下,到第二个位置。但是,它不会修改 begin 本身。

* 是一个解引用运算符,它本质上获取指向的值

begin is a iterator (think pointer), so begin + first advances to position of first.
or in the second case, to position of second. however, it does not modify begin itself.

* is a dereference operator, which essentially gets value being pointed to

谈下烟灰 2024-09-17 16:59:09

Operator() 基本上是为您的类定义“()”的功能。因此,在这种情况下,如果我们有名为“ic”的 IndexCompare 实例,我们可以编写“ic()”,并且将运行定义的功能,类似于函数调用和定义。

我希望这有助于理解运算符扩展。

The operator() is basically defining functionality for '()' with respect to your class. So in this case, if we had and IndexCompare instance named 'ic', we could write 'ic()' and the defined functionality would be run, similar to a function call and definition.

I hope that helps to understand the operator extension.

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