Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
类似于java';s字符串。在C++; < >我在C++中查找类似的函数,代码> String。它返回由指定分隔符剪切的字符串数组_C++_String_Split - Fatal编程技术网

类似于java';s字符串。在C++; < >我在C++中查找类似的函数,代码> String。它返回由指定分隔符剪切的字符串数组

类似于java';s字符串。在C++; < >我在C++中查找类似的函数,代码> String。它返回由指定分隔符剪切的字符串数组,c++,string,split,C++,String,Split,我认为其他堆栈溢出问题可能会回答这个问题: 总之,Java没有内置方法,但其中一位用户编写了类似的方法: 您可以使用strtok。 #包括 #包括 #包括 #包括 std::vector split(std::string str,std::string sep){ char*cstr=const_cast(str.c_str()); 字符*电流; std::载体arr; 电流=strtok(cstr,9月c_str()); while(当前!=NULL){ arr.push_back(当前

我认为其他堆栈溢出问题可能会回答这个问题:

总之,Java没有内置方法,但其中一位用户编写了类似的方法:

您可以使用strtok。

#包括
#包括
#包括
#包括
std::vector split(std::string str,std::string sep){
char*cstr=const_cast(str.c_str());
字符*电流;
std::载体arr;
电流=strtok(cstr,9月c_str());
while(当前!=NULL){
arr.push_back(当前);
current=strtok(NULL,sep.c_str());
}
返回arr;
}
int main(){
std::载体arr;
arr=split(“This--is--split”、“--”);

对于(size_t i=0;iI)我删除了它,因为我没有注意到标题中没有STROK(可能必须是STROK):DLook删除
[java]
标记,因为答案预计与java无关。
#include <string>
#include <vector>
#include <string.h>
#include <stdio.h>
std::vector<std::string> split(std::string str,std::string sep){
    char* cstr=const_cast<char*>(str.c_str());
    char* current;
    std::vector<std::string> arr;
    current=strtok(cstr,sep.c_str());
    while(current!=NULL){
        arr.push_back(current);
        current=strtok(NULL,sep.c_str());
    }
    return arr;
}
int main(){
    std::vector<std::string> arr;
    arr=split("This--is--split","--");
    for(size_t i=0;i<arr.size();i++)
        printf("%s\n",arr[i].c_str());
    return 0;
}