分解字符串

void str_split(const std::string& src, const std::string& sep, std::vector<std::string>& dst) {
std::string s;

for (std::string::const_iterator it = src.begin(); it != src.end(); it++)
{
    if (sep.find(*it) != std::string::npos)
    {
        if (s.length())
        {
            dst.push_back(s);
        }

        s.clear();
    }
    else
    {
        s += *it;
    }
}

if (s.length())
{
    dst.push_back(s);
}

}

例如:src = abcdefgh 碰到cf 字符串中的任意字符就切割 sep = cf 得到结果 dst = 'ab', 'de' , 'gh'

猜你喜欢

转载自blog.51cto.com/wenxuehui/2112701
今日推荐