67

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:

#include <boost/algorithm/string.hpp>

then:

vector<string> strs;
boost::split(strs,line,boost::is_any_of("\t"));

and the line is like

"test   test2   test3"

This is how I consume the result string vector:

void printstrs(vector<string> strs)
{
    for(vector<string>::iterator it = strs.begin();it!=strs.end();++it)
    {
        cout << *it << "-------";
    }

    cout << endl;
}

But why in the result strs I only get "test2" and "test3", shouldn't be "test", "test2" and "test3", there are \t (tab) in the string.

Updated Apr 24th, 2011: It seemed after I changed one line of code at printstrs I can see the first string. I changed

cout << *it << "-------";

to

cout << *it << endl;

And it seemed "-------" covered the first string somehow.

Noam M
  • 3,156
  • 5
  • 26
  • 41
icn
  • 17,126
  • 39
  • 105
  • 141

3 Answers3

92

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
yano
  • 4,827
  • 2
  • 23
  • 35
karlphillip
  • 92,053
  • 36
  • 243
  • 426
  • 2
    There's nothing else we can do for you if you won't share a minimal example that reproduces the problem you are facing. Remember: MINIMAL EXAMPLE, not your full app. – karlphillip Apr 20 '11 at 18:21
  • thanks. it seemed I changed cout<<*it<< "-------" to cout<<*it< – icn Apr 25 '11 at 04:39
  • 6
    FYI, following Boost headers are used in this sample: `#include ` and `#include ` – Richard Dally Jan 20 '16 at 09:07
  • This does not work. Try string = "hih1ihi" and substring = "hi. The result of the work is incorrect. – Optimus1 Nov 17 '21 at 14:47
15

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).

james
  • 186
  • 1
  • 3
3
template <class Container>
void split1(const std::string& str, Container& cont)
{
   boost::algorithm::split_regex(cont, str, boost::regex("\t"));
}

std::vector<std::string> vec1;
std::string str = "hest1\twest2\tpiest3";
split1(str, vec1);

vec == ("hest1","west2","piest3")

Danil
  • 701
  • 8
  • 7