c/c++ 字符串分割

c/c++ 字符串分割

vector split_string(const char *str, const char *pattern)
{
    char * strc =(char*)str;
    vector res;
    char* temp = strtok(strc, pattern);

    while(temp != NULL)
    {
        res.push_back(string(temp));
        temp = strtok(NULL, pattern);
    }

    return res;
}

vector split_string(const string &str, const string &pattern)
{
    char * strc = new char[strlen(str.c_str())+1];
    strcpy(strc, str.c_str());   //string转换成C-string
    vector res;
    char* temp = strtok(strc, pattern.c_str());
    while(temp != NULL)
    {
        res.push_back(string(temp));
        temp = strtok(NULL, pattern.c_str());
    }
    delete[] strc;
    return res;
}
#include 
#include 
#include 
#include 
#include 
#include 
#include

using namespace std;
vector split_string(const string &str, const string &pattern)
{
    char * strc = new char[strlen(str.c_str())+1];
    strcpy(strc, str.c_str());   //string转换成C-string
    vector res;
    char* temp = strtok(strc, pattern.c_str());
    while(temp != NULL)
    {
        res.push_back(string(temp));
        temp = strtok(NULL, pattern.c_str());
    }
    delete[] strc;
    return res;
}
int main(int argc, char *argv[])
{
        vector res;
        res = split_string("115223  rtmp://192.168.0.112/live/livestream"," ");
        cout<

shuchu:
115223 rtmp://192.168.0.112/live/livestream

你可能感兴趣的:(C++)