首页 > 编程语言 > 详细

c++ 字符串分割

时间:2016-02-21 22:29:34      阅读:358      评论:0      收藏:0      [点我收藏+]

void split(const string& str, vector<string>& ret_, string sep)--传入要分割的字符串str,要存储分割字符串的ret,分割的字符串str

{

    if (str.empty())

    {

        return;

    }

    

    string tmp;

    string::size_type pos_begin = str.find_first_not_of(sep);

    string::size_type comma_pos = 0;

    

    while (pos_begin != string::npos)

    {

        comma_pos = str.find(sep, pos_begin);

        if (comma_pos != string::npos)

        {

            tmp = str.substr(pos_begin, comma_pos - pos_begin);

            pos_begin = comma_pos + sep.length();

        }

        else

        {

            tmp = str.substr(pos_begin);

            pos_begin = comma_pos;

        }

        

        if (!tmp.empty())

        {

            ret_.push_back(tmp);

            tmp.clear();

        }

    }

}

c++ 字符串分割

原文:http://www.cnblogs.com/HemJohn/p/5205624.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!