Aw: char array nummterminated to std::vector<std::string>

Discussion in 'C++' started by Syron, Apr 22, 2011.

  1. Syron

    Syron Guest

    As the strings are separated by NUL chars and I simply assume that the final end of the char* will be marked by two NUL chars immediately following each other (thus the last string will be a "virtual" string with length 0), I suggest the following:

    typedef std::vector<std::string> StringVec;

    StringVec nulSplit(const char* input) {
    StringVec result;
    while(input[0] != 0) {
    std::string token(input);
    result.push_back(token);
    input += token.length()+1; // +1 to skip NUL char
    }
    return result;
    }
     
    Syron, Apr 22, 2011
    #1
    1. Advertising

  2. Syron

    Paul Guest

    Re: char array nummterminated to std::vector<std::string>

    "Syron" <> wrote in message
    news:...
    > As the strings are separated by NUL chars and I simply assume that the
    > final end of the char* will be >marked by two NUL chars immediately
    > following each other (thus the last string will be a "virtual" string
    > >with length 0), I suggest the following:

    Maybe there could also be three '\0's at the end, so you will still need to
    know the length of the input.
    In one of the posts the OP said he knows the length of the input c-strings
    so that length will be known
    ..
    >
    > typedef std::vector<std::string> StringVec;
    >
    > StringVec nulSplit(const char* input) {
    > StringVec result;
    > while(input[0] != 0) {
    > std::string token(input);
    > result.push_back(token);
    > input += token.length()+1; // +1 to skip NUL char
    > }
    > return result;
    > }
    >

    This actually does not work when I tried it.
    Each iteration of the while loop:
    creates a temp string object;
    invokes a call to push_back;
    invokes a call to length();

    But the use of any '\0' in the input c-string can be used to mark the size
    of the pushed string.
    Here is what I came up with when using this method:

    std::vector<std::string> split_str(char* in, int len){
    std::vector<std::string> out;
    if(*in != '\0')out.push_back(in);
    for(int i=1; i<len; ++i){
    if(in[i-1]=='\0'){
    if(in!= '\0')
    out.push_back(in+i);
    }
    }
    return out;
    }

    int main(){
    char arr[] = "\0dsfsf\0segser\0fwrwr\0\0grh\0grht\0";
    std::vector<std::string> v = split_str(arr, sizeof(arr));

    /*Code to display vector contents*/
    std::cout<<"size of vector = "<<v.size()<< std::endl;
    for(int i=0;i <v.size(); ++i){
    std::cout<< "v["<<i<<"] = " << v << std::endl;
    }
    /***** ******/
    }
     
    Paul, Apr 23, 2011
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Anonymous
    Replies:
    20
    Views:
    4,432
    Pete Becker
    Mar 30, 2005
  2. lovecreatesbeauty
    Replies:
    1
    Views:
    1,152
    Ian Collins
    May 9, 2006
  3. Replies:
    8
    Views:
    2,001
    Csaba
    Feb 18, 2006
  4. Replies:
    3
    Views:
    8,891
  5. Philipp Kraus
    Replies:
    28
    Views:
    1,155
    Tomasz Sowa
    Apr 28, 2011
Loading...

Share This Page