我使用boost::split
方法來分割字符串作爲這樣的:C++助推分割字符串
我首先要確保包括正確的頭能夠訪問boost::split
:
#include <boost/algorithm/string.hpp>
則:
vector<string> strs;
boost::split(strs,line,boost::is_any_of("\t"));
時線路等
"test test2 test3"
這是我如何消費的結果字符串向量:
void printstrs(vector<string> strs)
{
for(vector<string>::iterator it = strs.begin();it!=strs.end();++it)
{
cout << *it << "-------";
}
cout << endl;
}
但爲什麼在結果strs
我只得到"test2"
和"test3"
,不應該是"test"
,"test2"
和"test3"
,有\t
(標籤)的字符串。
更新於2011年4月24日:似乎在我改變了一行代碼printstrs
我可以看到第一個字符串。我改變
cout << *it << "-------";
到
cout << *it << endl;
,似乎"-------"
覆蓋的第一串莫名其妙。
顯示你如何使用矢量。我猜這個問題在那裏。 – 2011-04-20 17:44:11
'boost :: is_any_of(「\ t」)'效率低於'[](char c){return c =='\ t';}'。你只是想檢查一個可能性。 (不知道爲什麼沒有提升:是('\ t')') – MSalters 2013-08-26 10:33:35
@MSalters評論中的代碼是什麼意思?我如何使用該代碼來替代'boost :: is_any_of()'? – 2016-08-05 18:22:10