类似的功能在C ++到Python的strip()? [英] Similar function in C++ to Python's strip()?

查看:291
本文介绍了类似的功能在C ++到Python的strip()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Python中有一个非常有用的函数,名为 strip() 。任何类似的C ++?

There is a very useful function in Python called strip(). Any similar ones in C++?

推荐答案

我过去使用类似下面的东西:

There's nothing built-in; I used to use something like the following:

template <std::ctype_base::mask mask>
class IsNot
{
    std::locale myLocale;       // To ensure lifetime of facet...
    std::ctype<char> const* myCType;
public:
    IsNot( std::locale const& l = std::locale() )
        : myLocale( l )
        , myCType( &std::use_facet<std::ctype<char> >( l ) )
    {
    }
    bool operator()( char ch ) const
    {
        return ! myCType->is( mask, ch );
    }
};

typedef IsNot<std::ctype_base::space> IsNotSpace;

std::string
trim( std::string const& original )
{
    std::string::const_iterator right = std::find_if( original.rbegin(), original.rend(), IsNotSpace() ).base();
    std::string::const_iterator left = std::find_if(original.begin(), right, IsNotSpace() );
    return std::string( left, right );
}

(我现在有一个显着更复杂的
版本,正确处理UTF-8。)

which works pretty well. (I now have a significantly more complex version which handles UTF-8 correctly.)

这篇关于类似的功能在C ++到Python的strip()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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