首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

Trim a string with C++ [二]

2012-11-26 
Trim a string with C++ [2]Ive wrote a blog to discuss how to trim a string in C. Now I find an oth

Trim a string with C++ [2]

I've wrote a blog to discuss how to trim a string in C++. Now I find an other way in high performance, see the implements below: 

#include <string>#define  STRING_TRIM_DROPS    " \t"std::string& trim(std::string &s){    static std::string drops = STRING_TRIM_DROPS;  // A char list specifies which characters should be trimmed.    if (s.empty())    {        return s;    }    s.erase(0, s.find_first_not_of(drops));    s.erase(s.find_last_not_of(drops) + 1);    return s;}

Yes, using string::erase() method is the fastest way I ever met.

热点排行