帮助理解 C++、模板、operator() 的类示例代码
我不确定下面的类到底是做什么的,我们有一个类示例。在下面的代码中,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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您问
operator()
的作用,它允许您像函数一样调用对象。有关示例,请参阅本文。如果您询问示例中的函数正在做什么,它会比较由传递给函数的索引指定的两个元素的值。
begin + first
指的是从迭代器begin
开始的索引first
处的元素,类似地begin + secondary
。*(begin + first)
获取该位置的值。您可以通过传入一对迭代器将此类与任何 STL 容器(支持随机访问)一起使用。例如,您可以将其与如下向量一起使用:
例如,现在调用
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 indexfirst
starting from the iteratorbegin
, similarlybegin + 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:
Now calling
compare(2, 5)
for example would compare the values ofvec[2]
andvec[5]
.begin
是一个迭代器(认为是指针),因此begin + first
前进到第一个位置。或者在第二种情况下,到第二个位置。但是,它不会修改
begin
本身。*
是一个解引用运算符,它本质上获取指向的值begin
is a iterator (think pointer), sobegin + 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 toOperator() 基本上是为您的类定义“()”的功能。因此,在这种情况下,如果我们有名为“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.