按类型排序矢量&按类型搜索或派生类型

时间:2017-08-17 11:53:57

标签: c++

我想按类型排序和搜索向量(所有都继承自相同的基础)。到目前为止,我一直在使用class Base {}; class DerivedA : public Base {}; class DerivedA1 : public DerivedA {}; class DerivedA1I : public DerivedA1 {}; // Calling this looks for a DerivedA, but can also return a DerivedA1 OR DerivedA1I DerivedA* result = search<DerivedA>(); 来比较类型。我的代码看起来像

//Retrieves all the root object ids of the documents version tree where there are duplicates as their name. 
//Here I thought the latest created document is the one to keep, you can adapt it to your needs - **see ORDER by clause**.

    select i_chronicle_id from dm_document where folder('/XXX/YYYY', DESCEND) and object_name in (select object_name from dm_document where folder('/XXX/YYYY', DESCEND) and object_name = 'abc.pdf' group by object_name having count(*) > 1) order by r_creation_date asc;

到目前为止,这个工作正常,但我现在希望我的搜索方法也返回从它搜索的那个派生的任何类型,我不知道如何去做。例如

//"pseudo-code" - deletes the entire version tree of the document that is not the last in the returned list
while 'not last i_chronicle_id from the result collection' 
     //execute this dql: 
     `delete dm_document (all) objects where i_chronicle_id='<i_chronicle_id from the list>';`

1 个答案:

答案 0 :(得分:1)

如果Base是多态的,您可以这样做:

std::vector<Base*> bases;

auto it = std::find(bases.begin(), bases.end(),
                    [](const auto* base) {
                        return dynamic_cast<const DerivedA*>(base) != nullptr;
                    });