码迷,mamicode.com
首页 > 其他好文 > 详细

split函数实现

时间:2019-05-23 21:21:29      阅读:121      评论:0      收藏:0      [点我收藏+]

标签:str   substr   ++   void   ack   main   pause   dong   code   

 

#include <iostream>
#include <sstream>
#include <vector>
#include <string>

using namespace std;

void addstr(const string s, int i, int j, vector<string> &result)
{//从i到分割点j部分子串加入到result,需判断子串是否全为空,有一个不为空则可以
    bool allempty = true;
    for (int x = i; x < j ; x++)
    {//判断子串是否全为空,如果有一个字符不为空,则加入到result
        if (s[x] !=  )
        {
            allempty = false;
            break;
        }
    }
    if (!allempty)
        result.push_back(s.substr(i, j - i));//从i开始,j-i个字符
}
vector<string> mysplit(const string s, const char delim)
{
    vector<string> result;
    if (s.empty()) return result;
    int i = 0;
    int j = 0;
    while (j < s.size() && s[j] ==  )
    {//移动到第一个不为空的字符处
        ++i;
        ++j;
    }
    //++j;
    while (j < s.size())
    {//从i到分割点j 放入result中
        if (s[j] == delim)
        {
            addstr(s, i, j, result);
            i = ++j;//i和j都指向下一个字符
        }
        else
            ++j;
    }
    addstr(s, i, j, result);//最后一个子串
    return result;
}


int main() {
    string s = ",he  llo boy, I am a student, come from shangdong";
    vector<string> res1 = mysplit(s, ,);
    for (auto a : res1) {

        cout << a << endl;
    }
    system("pause");
    return 0;
}

 

split函数实现

标签:str   substr   ++   void   ack   main   pause   dong   code   

原文地址:https://www.cnblogs.com/beixiaobei/p/10914239.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!