std::is_empty
是一个特征模板。 std::is_empty<X>
告诉您该类型 X
是否为空类型——也就是说,它是 astruct {}
或class {}
其中没有任何内容。
这不是你想要的。首先,因为它不能以你需要的方式被调用——它没有operator()
对你有用的东西——第二,它接受一个类型,而不是一个类型的实例,并回答关于类型的问题,而不是实例。
auto list_is_empty = [](std::list<pcl::PointXYZRGB> const& l) {
return l.empty();
};
tab.erase(std::remove_if(tab.begin(), tab.end(), list_is_empty), tab.end());
将解决您的问题。解决它的更好方法是:
struct container_is_empty_t {
template<class C>
bool operator()(C const& c)const{
return c.empty();
}
// not really needed, arrays of size 0 are non-standard
template<class T, size_t N>
bool operator()(T(&)[N])const{
return N > 0;
}
};
static container_is_empty_t const container_is_empty;
在一些带有实用程序代码的头文件中,然后:
tab.erase(std::remove_if(tab.begin(), tab.end(), container_is_empty), tab.end());
做同样的事情,但没有被硬编码为只在std::list< pcl::PointXYZRGB>
s 上工作。
或在c++14中:
tab.erase(std::remove_if(tab.begin(), tab.end(), [](auto&& c){ return c.empty(); }, tab.end());
警告
在执行擦除删除习语时,如果您忘记了该tab.end()
部分,您将获得可以编译但以病态方式做完全错误的事情的代码。
我觉得最好有
// erases elements from sequential container C if f(element) is true:
template<class C, class F>
auto erase_if( C& c, F f ) {
using std::begin; using std::end;
auto it = std::remove_if( begin(c), end(c), std::move(f) );
auto sz = std::distance( it, end(c) );
c.erase( it, end(c) );
return sz;
}
辅助函数来避免这种危险。
然后你得到:
erase_if( tab, [](auto&& c){ return c.empty(); } );
这很好而且很短。
c++20标准增加std::erase_if
了语言。