c++ - 我不明白 ptr_fun

标签 c++ string stl

我在一段 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;
}

最佳答案

来自 Scott Meyers 的 Effective STL(第 40 项):

The only thing ptr_fun does is make some typedefs available. That's it. These typedefs are required by not1, and that's why applying not1 to ptr_fun but applying not1 to ... directly doesn't work. ... lacks the typedefs that not1 demands.

这与 Praetorian 在上面的评论中给出的答案基本相同,但 Effective STL 提供了更一般的解释。

关于c++ - 我不明白 ptr_fun,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34010925/

相关文章:

c++ - 在 C++ 中生成 N 选择 K 排列

java - 在 hibernate 中调用返回 varchar 的 mysql 函数

c++ - 无法从基于 libstdc++ 策略的数据结构中删除

c++ - 在 map 中包含 map 的结构? (C++/STL)

c++ - 如何在 Qmap 中查找特定值

c++ - Boost.Regex 分隔符解析

c++ - 为什么这段代码在 ROOT 下编译,但没有做任何它想做的事情?

C - 以文本作为参数

python - 解码Python字符串

c++ - 为什么 std::binary_search 使用 ForwardIterator 而不是 RandomIterator?