struct mycompare和static bool mycompare有什么区别?
What's the difference between struct mycompare and static bool mycompare?
struct mycompare1 {
bool operator() (const Interval a, const Interval b) {
return a.start < b.start;
}
} mycompare1_instance;
static bool mycompare2(const Interval a, const Interval b) {
return a.start < b.start;
}
Q1:这两者有什么区别。似乎mycompare1_instance以某种方式等于这样的mycompare2。以下两行似乎做同样的事情。
sort(intervals.begin(), intervals.end(), mycompare1);
sort(intervals.begin(), intervals.end(), mycompare2);
谁能解释一下?
Q2:"静态"在这里有什么用?
谢谢你!
第一种情况定义了 operator(),因此您可以将其用作函数 - 简而言之,它们是函子。优点是它可以有一个状态,并且根据您定义 operator() 的方式,您可以将该类的实例用作函数。第二种情况只是一个静态函数,不保存类/结构意义上的状态。
我怀疑这个问题与函子有关
即你见过的
mycompare_instance1(x,y)
与
mycompare2(x,y)
第一个是函子。它用于我们需要制作既是具有状态的对象,又可以像函数一样调用的东西。第二个是简单的函数
相关文章:
- 为不同配置设置MSVC_RUNTIME_LIBRARY的正确方法是什么
- 警告处理为错误这里有什么问题
- 什么时候调用组成单元对象的析构函数
- #定义c-预处理器常量..我做错了什么
- 努力将整数转换为链表。不知道我在这里做错了什么
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 什么时候在C++中返回常量引用是个好主意
- 当在同一名称空间中有两个具有相同签名的函数时,会发生什么
- C++避免重复声明的语法是什么
- c++库的公共头文件中应该包含什么
- 问题:什么是QAbstractItemView::NoEditTriggers的反面
- 有什么方法可以遍历结构吗
- 当类在C++中定义时,有什么方法可以"register"类吗?
- ifstream什么都没读
- 在C++中,将大的无符号浮点数四舍五入为整数的最佳方法是什么
- 实现无开销push_back的最佳方法是什么
- C++从另一个类访问公共静态向量的正确方法是什么
- "throw expression code" 1e7 >返回 d 是什么?投掷标准::overflow_error( "too big" ) : d;意味 着?
- 我应该使用什么来代替void作为变体中的替代类型之一
- struct mycompare和static bool mycompare有什么区别?