c++ - c++17 的 std::ptr_fun 替换

标签 c++ c++17

我正在使用 std::ptr_fun 如下:

static inline std::string &ltrim(std::string &s) {
    s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not1(std::ptr_fun<int, int>(std::isspace))));
    return s;
}

this answer 中所述.

但是,这不能使用 C++17(使用 Microsoft Visual Studio 2017)编译,并出现错误:

error C2039: 'ptr_fun': is not a member of 'std'

如何解决这个问题?

最佳答案

您使用 lambda:

static inline std::string &ltrim(std::string &s) {
    s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](int c) {return !std::isspace(c);}));
    return s;
}

您引用的答案来自 2008 年,远在 C++11 和 lambda 出现之前。

关于c++ - c++17 的 std::ptr_fun 替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44973435/

相关文章:

c++ - 在 C++ 中高效地读取一个非常大的文本文件

C++ 库和自注册类 : Factory map empty in client application

c++ - 别名标准模板函数

c++ - 从一个不再存在的函数创建的 lambda 的内部框架修改 via 闭包中的变量是否安全

C++ 相当于 python 的 itertools::cycle

c++ - 运行时大小的数组和指针衰减

c++ - 类比理解C++17中的结构化绑定(bind)

C++ 缩短后续函数调用

c++ - "if constexpr"与 "try in constexpr function"警告交互

c++ - 嵌入式系统的谷歌测试