std :: ptr_fun替换为c ++ 17 [英] std::ptr_fun replacement for c++17

查看:516
本文介绍了std :: ptr_fun替换为c ++ 17的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 静态内联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;
}

此答案



但是此方法无法与C ++ 17一起编译(使用Microsoft Visual Studio 2017),错误为:

 错误C2039:'ptr_fun':不是'std'
<的成员/ pre>

如何解决?

解决方案

您使用一个lambda:

 静态内联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和lambdas存在。


I am using std::ptr_fun as follows:

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;
}

as presented in this answer.

However this does not compile with C++17 (using Microsoft Visual Studio 2017), with the error:

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

How can this be fixed?

解决方案

You use a 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;
}

The answer you cited is from 2008, well before C++11 and lambdas existed.

这篇关于std :: ptr_fun替换为c ++ 17的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆