Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/145.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在容器中查找派生对象_C++_Oop_Vector - Fatal编程技术网

C++ 在容器中查找派生对象

C++ 在容器中查找派生对象,c++,oop,vector,C++,Oop,Vector,假设我有基本类东西,从中我有鞋,裤子,衬衫。然后我有一个向量柜 如何找到我的衣柜中有多少衬衫?与lambda一起使用,lambda使用a来确定每个元素是否指向衬衫(或其子类型--这也将捕获TShirt对象,其中TShirt是继承衬衫的类): auto shirts=std::count\u如果( 标准::开始(壁橱), 标准::末端(壁橱), [](事物常数*事物){ 返回动态_cast(thing)!=nullptr; } ); 与lambda一起使用,lambda使用a来确定每个元素是否指向

假设我有基本类
东西
,从中我有
裤子
衬衫
。然后我有一个向量柜

如何找到我的
衣柜中有多少
衬衫

与lambda一起使用,lambda使用a来确定每个元素是否指向
衬衫
(或其子类型--这也将捕获
TShirt
对象,其中
TShirt
是继承
衬衫
的类):

auto shirts=std::count\u如果(
标准::开始(壁橱),
标准::末端(壁橱),
[](事物常数*事物){
返回动态_cast(thing)!=nullptr;
}
);
与lambda一起使用,lambda使用a来确定每个元素是否指向
衬衫
(或其子类型——这也将捕获
TShirt
对象,其中
TShirt
是继承
衬衫
的类):

auto shirts=std::count\u如果(
标准::开始(壁橱),
标准::末端(壁橱),
[](事物常数*事物){
返回动态_cast(thing)!=nullptr;
}
);

假设我想找到
尺寸为
m_
>=40的
裤子,我如何将该功能整合到lambda中?@HichigayaHachiman的方法完全相同<代码>[](Thing const*Thing){auto pants=dynamic_cast(Thing);return(pants!=nullptr)和&(pants->m_size>=40)}
假设我想找到
裤子,它们是
m_size
=40,我如何将该函数合并到lambda中?@HichigayaHachiman完全相同的方式<代码>[](Thing const*Thing){auto pants=dynamic_cast(Thing);return(pants!=nullptr)&&(pants->m_size>=40);}
auto shirts = std::count_if(
    std::begin(closet),
    std::end(closet),
    [] (Thing const *thing) {
        return dynamic_cast<Shirt const *>(thing) != nullptr;
    }
);