c++ boost split string

后端 未结 2 1357
予麋鹿
予麋鹿 2020-12-13 03:51

I\'m using the boost::split method to split a string as this:

I first make sure to include the correct header to have access to boost::split

相关标签:
2条回答
  • 2020-12-13 04:03

    My best guess at why you had problems with the ----- covering your first result is that you actually read the input line from a file. That line probably had a \r on the end so you ended up with something like this:

    -----------test2-------test3

    What happened is the machine actually printed this:

    test-------test2-------test3\r-------

    That means, because of the carriage return at the end of test3, that the dashes after test3 were printed over the top of the first word (and a few of the existing dashes between test and test2 but you wouldn't notice that because they were already dashes).

    0 讨论(0)
  • 2020-12-13 04:09

    The problem is somewhere else in your code, because this works:

    string line("test\ttest2\ttest3");
    vector<string> strs;
    boost::split(strs,line,boost::is_any_of("\t"));
    
    cout << "* size of the vector: " << strs.size() << endl;    
    for (size_t i = 0; i < strs.size(); i++)
        cout << strs[i] << endl;
    

    and testing your approach, which uses a vector iterator also works:

    string line("test\ttest2\ttest3");
    vector<string> strs;
    boost::split(strs,line,boost::is_any_of("\t"));
    
    cout << "* size of the vector: " << strs.size() << endl;
    for (vector<string>::iterator it = strs.begin(); it != strs.end(); ++it)
    {
        cout << *it << endl;
    }
    

    Again, your problem is somewhere else. Maybe what you think is a \t character on the string, isn't. I would fill the code with debugs, starting by monitoring the insertions on the vector to make sure everything is being inserted the way its supposed to be.

    Output:

    * size of the vector: 3
    test
    test2
    test3
    
    0 讨论(0)
提交回复
热议问题