【发布时间】:2015-12-01 01:09:13
【问题描述】:
我在其中一个 c++ 代码中遇到了这个 ptr_fun 的东西,我尝试从 cplusplus.com 阅读它,但老实说,我无法弄清楚这个函数指针应该做什么。
感兴趣的代码非常简单,将字符串开头的空白部分剪掉。
static inline string & trim_beg(string & s) {
s.erase(s.begin(), find_if(s.begin(), s.end(), not1(ptr_fun < int, int > (isspace))));
return s;
}
【问题讨论】:
-
您不必指定模板参数,
ptr_fun(::isspace)应该这样做。ptr_fun的唯一目的是包装isspace并提供unary_negate所需的成员typedefargument_type(not1返回的东西)。所以find_if调用将返回一个迭代器,指向!isspace为真的第一个元素。