使用std::count_if与使用的λ 动态垂头丧气,以确定是否每一个元素指向一个Shirt(或它们的子类型-这也将赶上,比方说,TShirt对象,其中TShirt是继承的类Shirt):
auto shirts = std::count_if(
std::begin(closet),
std::end(closet),
[] (Thing const *thing) {
return dynamic_cast<Shirt const *>(thing) != nullptr;
}
);
Run Code Online (Sandbox Code Playgroud)