用C++实现split/startswith/endswith/trim

来源:互联网 发布:招聘网络推销员 编辑:程序博客网 时间:2024/06/06 07:09

一.split函数

1.python演示
这里写图片描述
2.C++函数原型

vector<string> split(const string& srcstr, const string& delimeter = ";");//split the string by delimeter

3.实现思路
用delimeter分割符将字符串srcstr分割开,结果保存到向量中,默认的分割符为”;”。思路是,

(1)find_first_not_of(delimeter)即为开始切割的位置pos_begin
(2)随后find(delimeter),找到第一个分隔符的位置dlm_pos
(3)那么加在dlm_pos和pos_begin中间的字符串为第一个结果,保存在vector中
(4)以此循环,更新pos_begin和dlm_pos,直到pos_begin超出srcstr的范围,即到达string::npos

4.源码展示

//strutils.cppvector<string> split(const string& srcstr, const string& delimeter){    vector<string> ret(0);//use ret save the spilted reault    if(srcstr.empty())    //judge the arguments    {        return ret;    }    string::size_type pos_begin = srcstr.find_first_not_of(delimeter);//find first element of srcstr    string::size_type dlm_pos;//the delimeter postion    string temp;              //use third-party temp to save splited element    while(pos_begin != string::npos)//if not a next of end, continue spliting    {        dlm_pos = srcstr.find(delimeter, pos_begin);//find the delimeter symbol        if(dlm_pos != string::npos)        {            temp = srcstr.substr(pos_begin, dlm_pos - pos_begin);            pos_begin = dlm_pos + delimeter.length();        }        else        {            temp = srcstr.substr(pos_begin);            pos_begin = dlm_pos;        }        if(!temp.empty())            ret.push_back(temp);    }    return ret;}

二.startswith函数

1.python展示
这里写图片描述
2.C++函数原型

bool startswith(const std::string& str, const std::string& start)

3.实现思路

str截取出前len个字符与start比较(len为start字符串的长度)

4.代码展示

//strutils.cppbool startswith(const std::string& str, const std::string& start){    int srclen = str.size();    int startlen = start.size();    if(srclen >= startlen)    {        string temp = str.substr(0, startlen);        if(temp == start)            return true;    }    return false;}

三.endswith函数

1.python展示
这里写图片描述
2.C++函数原型

bool endswith(const std::string& str, const std::string& end)

3.实现思路

str截取出最后len个字符与start比较(len为start字符串的长度)

4.代码展示

bool endswith(const std::string& str, const std::string& end){    int srclen = str.size();    int endlen = end.size();    if(srclen >= endlen)    {        string temp = str.substr(srclen - endlen, endlen);        if(temp == end)            return true;    }    return false;}

四.trim函数

1.python演示
这里写图片描述
注:python中strip是trim掉字符串两边的空格。
lstrip, trim掉左边的空格
rstrip, trim掉右边的空格
2.C++函数原型

string trim(const std::string& str)

3.实现思路

查找第一个不是空格或者制表符的位置pos_begin,查找最后一个不是空格和制表符的位置pos_end,那么夹在两者之间的即为trim掉字符串两边空格和\t的新字符串

4.代码展示

string trim(const std::string& str){    string ret;    //find the first position of not start with space or '\t'    string::size_type pos_begin = str.find_first_not_of(" \t");    if(pos_begin == string::npos)        return str;    //find the last position of end with space or '\t'    string::size_type pos_end = str.find_last_not_of(" \t");    if(pos_end == string::npos)        return str;    ret = str.substr(pos_begin, pos_end-pos_begin);    return ret;}
1 0