C++ boost拆分字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5734304/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
c++ boost split string
提问by icn
I'm using the boost::split
method to split a string as this:
我正在使用该boost::split
方法来拆分字符串,如下所示:
I first make sure to include the correct header to have access to boost::split
:
我首先确保包含正确的标头以访问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.
但是为什么在结果中strs
我只得到"test2"
and "test3"
,不应该是"test"
, "test2"
and "test3"
,\t
字符串中有(tab) 。
Updated Apr 24th, 2011:It seemed after I changed one line of code at printstrs
I can see the first string. I changed
2011 年 4 月 24 日更新:似乎在我更改了一行代码之后printstrs
我可以看到第一个字符串。我变了
cout << *it << "-------";
to
到
cout << *it << endl;
And it seemed "-------"
covered the first string somehow.
它似乎"-------"
以某种方式覆盖了第一根弦。
回答by karlphillip
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.
同样,您的问题出在其他地方。也许你认为是\t
字符串上的一个字符,不是。我会用调试填充代码,首先监视向量上的插入,以确保所有内容都按预期方式插入。
Output:
输出:
* size of the vector: 3
test
test2
test3
回答by james
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:
我对为什么 ----- 覆盖第一个结果有问题的最佳猜测是,您实际上是从文件中读取输入行。该行的末尾可能有一个 \r,所以你最终得到了这样的结果:
-----------test2-------test3
-----------test2-------test3
What happened is the machine actually printed this:
发生的事情是机器实际上打印了这个:
test-------test2-------test3\r-------
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).
这意味着,由于 test3 末尾的回车,test3 之后的破折号打印在第一个单词的顶部(以及 test 和 test2 之间的一些现有破折号,但您不会注意到,因为它们是已经破折号)。