我遇到了一个问题.我有一个A类,一个继承自A的类,我们称之为B类.我有虚函数.我想通过operator ==将A和B与另一个C类进行比较.如果我想要一个A的列表,让我们说在stl列表中,我必须使用指向A的指针,所以它看起来像:
list<*A> list;
Run Code Online (Sandbox Code Playgroud)
我也有: C something
但现在,我无法使用该函数:find(list.begin(),list.end(),something)
因为我不能使用operator ==指针(*).
我找到了一个解决方案,但我认为它不是最好的,所以我的问题是 - 我可以做得更好吗?
iter=list.begin();
for(iter;iter!=list.end();++iter)
{
if((*iter).operator==(something)
return ...
}
Run Code Online (Sandbox Code Playgroud)
谢谢.
您可以使用find_if,它允许您提供检查相等值的函数.
auto check_something =
[&something](const list<*A>::iterator& iter){return *iter == something; };
find_if(list.begin(),list.end(),check_something)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
170 次 |
| 最近记录: |