如何(C++ STL)binary_search 抽象类?
可以使用 STL 二分搜索算法(binary_search、upper_bound、lower_bound)在基指针向量中搜索派生对象,如下所示。由于 Base 是抽象的(受保护的构造函数),因此必须为搜索函数实例化 Derived 对象,这有点难看。
我想在向量中搜索给定时间以上的第一个 Derived 。我可以在不任意选择和实例化我的许多继承类之一的情况下执行此操作吗?
#include <algorithm>
#include <vector>
#include <stdio.h>
using namespace std;
class Base {
protected:
Base(double t, int d) : data(d), time(t) {}
public:
double time;
int data;
virtual void print() {
printf("Base: data = %d, time = %.1f\n",data,time);
}
};
class Derived : public Base {
public:
Derived(double t, int d) : Base(t,d) {}
virtual void print() {
printf("Derived: data=%d, time=%.1f\n",data,time);
}
};
struct BaseTimeComp {
bool operator()(Base* a, Base* b) { return a->time < b->time; }
};
int main()
{
vector<Base*> v;
for(int i=0; i<5; i++) { v.push_back(new Derived(i+0.4,i)); }
Base* pLow = *(lower_bound(v.begin(),v.end(),
new Derived(3.5,0), //NOT "new Base(3.5,0)"
BaseTimeComp()));
printf("lower bound for time=3.5:\n");
pLow->print();
}
程序打印: 时间的下限=3.5: 推导:数据=4,时间=4.4
One can use the STL binary search algorithms (binary_search, upper_bound, lower_bound) to search a vector of Base pointers for a derived object, as shown below. Since Base is abstract (protected constructor), one has to instantiate a Derived object for the search functions, which is slightly ugly.
I want to search the vector for the first Derived above a given time. Can I do this without arbitrarily picking and instantiating one of my many inherited classes?
#include <algorithm>
#include <vector>
#include <stdio.h>
using namespace std;
class Base {
protected:
Base(double t, int d) : data(d), time(t) {}
public:
double time;
int data;
virtual void print() {
printf("Base: data = %d, time = %.1f\n",data,time);
}
};
class Derived : public Base {
public:
Derived(double t, int d) : Base(t,d) {}
virtual void print() {
printf("Derived: data=%d, time=%.1f\n",data,time);
}
};
struct BaseTimeComp {
bool operator()(Base* a, Base* b) { return a->time < b->time; }
};
int main()
{
vector<Base*> v;
for(int i=0; i<5; i++) { v.push_back(new Derived(i+0.4,i)); }
Base* pLow = *(lower_bound(v.begin(),v.end(),
new Derived(3.5,0), //NOT "new Base(3.5,0)"
BaseTimeComp()));
printf("lower bound for time=3.5:\n");
pLow->print();
}
The program prints:
lower bound for time=3.5:
Derived: data=4, time=4.4
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
比较的目标不必与容器内容的类型相同,它只需是您可以与容器进行比较的内容:
您假设必须做出某种
Base
是错误的。只要显式(或隐式)比较运算符知道要做什么,您就可以定义适合您比较的BaseKey
。下面的注释也是错误的,正如这个更复杂的示例所示:
您还可以显式使用比较类型(这有助于解决操作顺序问题,例如您可能会在
upper_bound
中发现的问题):A
binary_search
示例提供了与多态性的比较:The target of the comparison doesn't have to be the same type as the contents of the container, it just has to be something you can compare to the container:
Your assumption that you have to make some kind of
Base
is wrong. You can define aBaseKey
which is suitable for your comparisons as long as your explicit (or implied) comparison operator knows what to do.The comment below is also wrong, as this more complex example demonstrates:
You can also use a comparision type explicitly (which helps with order of operations problems such as you might find with
upper_bound
):A
binary_search
example providing both comparisons with polymorphism:您可以传递一个空指针,并设计比较函数忽略它,并仅测试另一个对象的特定属性。
You could pass a null pointer, and design your comparison function ignore it, and only test the other object for a specific attribute.
在某种程度上,您可以通过使用静态方法:
并在对 LowerBound 的调用中:
这意味着您不必了解任何派生类,但是获取 Base 类型的实例会击败Base 的目的首先是抽象的。您也可以将构造函数公开。
You could, in a way, by using a static method:
and in the call to LowerBound:
This means you don't have to have knowledge of any of the derived classes, but getting an instance of Base kind of defeats the purpose of Base being abstract in the first place. You would just as well make the constructor public.