Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/139.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
C++ 将字符数组拆分为两个字符数组_C++_C_Split_Char - Fatal编程技术网

C++ 将字符数组拆分为两个字符数组

C++ 将字符数组拆分为两个字符数组,c++,c,split,char,C++,C,Split,Char,我想将一个包含两个由“|”分隔的“字符串”的字符数组拆分为两个字符数组 这是我的示例代码 void splitChar(const char *text, char *text1, char *text2) { for (;*text!='\0' && *text != '|';) *text1++ = *text++; *text1 = '\0'; for (;*++text!='\0';) *text2++ = *text; *text2 = '\0

我想将一个包含两个由“|”分隔的“字符串”的字符数组拆分为两个字符数组

这是我的示例代码

void splitChar(const char *text,  char *text1, char *text2)
{
   for (;*text!='\0' && *text != '|';) *text1++ = *text++;
   *text1 = '\0';
   for (;*++text!='\0';) *text2++ = *text;
   *text2 = '\0';
}

int main(int argc, char* argv[])
{

    char *text = "monday|tuesday", text1[255], text2 [255];
    splitChar (text, text1, text2);
    return 0;
}
我有两个问题:

  • 如何进一步改进C语言中的这段代码(例如,在1中重新编写循环代码)

  • 如何在C++中修改此代码?


    可能有一个解决方案是:

    如果你想用C++编写它,使用STL < /P>
    string s = "monday|tuesday";  
    int pos = s.find('|');  
    if(pos == string::npos)  
        return 1;  
    string part1 = s.substr(0, pos);  
    string part2 = s.substr(pos+1, s.size() - pos);
    

    看看这里给出的例子:

    我不知道a),但对于B),这里有一个我在各种项目中使用的实用程序库中的方法,展示了如何将任意数量的单词拆分为
    向量。它被编码为按空格和制表符拆分,但如果需要,可以将其作为附加参数传入。它返回拆分的字数:

    unsigned util::split_line(const string &line, vector<string> &parts)
    {
        const string delimiters = " \t";
        unsigned count = 0;
        parts.clear();
    
        // skip delimiters at beginning.
        string::size_type lastPos = line.find_first_not_of(delimiters, 0);
    
        // find first "non-delimiter".
        string::size_type pos = line.find_first_of(delimiters, lastPos);
    
        while (string::npos != pos || string::npos != lastPos)
        {
            // found a token, add it to the vector.
            parts.push_back(line.substr(lastPos, pos - lastPos));
            count++;
    
            // skip delimiters.  Note the "not_of"
            lastPos = line.find_first_not_of(delimiters, pos);
    
            // find next "non-delimiter"
            pos = line.find_first_of(delimiters, lastPos);
        }
    
        return count;
    }
    
    unsigned util::split_行(常量字符串和行、向量和部分)
    {
    常量字符串分隔符=“\t”;
    无符号计数=0;
    零件。清除();
    //跳过开头的分隔符。
    字符串::size\u type lastPos=line.find\u first\u not\u of(分隔符,0);
    //查找第一个“非分隔符”。
    字符串::size\u type pos=line.find\u first\u of(分隔符,lastPos);
    while(string::npos!=pos | | string::npos!=lastPos)
    {
    //找到一个标记,将其添加到向量。
    零件。推回(行。substr(lastPos,pos-lastPos));
    计数++;
    //跳过分隔符。请注意“not_of”
    lastPos=行。首先查找(分隔符,pos)中的\u而不是\u;
    //查找下一个“非分隔符”
    pos=行。查找(分隔符,lastPos)中的第一个;
    }
    返回计数;
    }
    
    对于A,使用内部库:

    void splitChar(const char *text,  char *text1, char *text2)
    {
        int len = (strchr(text,'|')-text)*sizeof(char);
        strncpy(text1, text, len);
        strcpy(text2, text+len+1);
    }
    

    我发现破坏性拆分是性能和灵活性的最佳平衡

    void split_destr(std::string &str, char split_by, std::vector<char*> &fields) {
        fields.push_back(&str[0]);
        for (size_t i = 0; i < str.size(); i++) {
            if (str[i] == split_by) {
                str[i] = '\0';
                if (i+1 == str.size())
                    str.push_back('\0');
                fields.push_back(&str[i+1]);
            }
        }
    }
    
    void split_dest(std::string和str、char split_by、std::vector和fields){
    fields.push_back(&str[0]);
    对于(大小i=0;i
    然后是针对懒惰的非破坏性版本

    template<typename C>
        void split_copy(const std::string &str_, char split_by, C &container) {
            std::string str = str_;
            std::vector<char*> tokens;
            parse::split_destr(str, split_by, tokens);
            for (size_t i = 0 ; i < tokens.size(); i++)
                container.push_back(std::string( tokens[i] ));
        }
    
    模板
    无效拆分副本(常量std::string和str、字符拆分依据、C和容器){
    std::string str=str;
    std::向量标记;
    split_dest(str、split_by、tokens);
    对于(size_t i=0;i

    我是在boost::Tokenizer之类的东西在处理gb+大小的文件时遇到这种情况的。

    我为我的回答提前道歉:)任何人都不应该在家里尝试这种方法

    回答你问题的第一部分

    A] 如何进一步改进C语言中的这段代码(例如,在1中重新编写循环代码)

    此算法的复杂性将取决于“|”在字符串中的位置,但此示例仅适用于由“|”分隔的两个字符串。您可以在以后轻松地修改它以获得更多信息

    #include <stdio.h>
    
    void splitChar(char *text,  char **text1, char **text2)
    {
        char * temp = *text1 = text;
        while (*temp != '\0' && *temp != '|') temp++;
    
        if (*temp == '|') 
        {
            *temp ='\0';
            *text2 = temp + 1;
        }
    }
    
    int main(int argc, char* argv[])
    {
    
        char text[] = "monday|tuesday", *text1,*text2;
        splitChar (text, &text1, &text2);
        printf("%s\n%s\n%s", text,text1,text2);
        return 0;
    }
    
    #包括
    void splitChar(char*text,char**text1,char**text2)
    {
    char*temp=*text1=text;
    而(*temp!='\0'&&&*temp!='|')temp++;
    如果(*temp=='|')
    {
    *温度='\0';
    *text2=温度+1;
    }
    }
    int main(int argc,char*argv[])
    {
    char text[]=“星期一|星期二”、*text1、*text2;
    splitChar(文本、文本1和文本2);
    printf(“%s\n%s\n%s”,text,text1,text2);
    返回0;
    }
    
    这是因为c样式数组使用空字符来终止字符串。由于使用“”初始化字符串将在末尾添加一个空字符,因此您只需将出现的“|”替换为空字符,并将其他字符指针指定给“|”之后的下一个字节


    您必须确保使用[]初始化原始字符串,因为这会告诉编译器为字符数组分配存储空间,其中char*可能会在无法更改的静态内存区域中初始化字符串。

    您的意思是“字符串”,而不是“char”…不要执行
    pos=-1
    。使用
    pos==std::string::npos
    为了正确起见,
    len
    应该是
    ptrdiff\u t
    类型。