以下 C++ 程序
#include <iostream>
#include <boost/tokenizer.hpp>
using namespace std;
int main()
{
typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
string s ("(0,30,0)");
tokenizer tokens(s.substr(1,s.size()-2), boost::char_separator<char>(","));
for (tokenizer::iterator tok_iter = tokens.begin(); tok_iter != tokens.end(); ++tok_iter)
cout<<*tok_iter<<endl;
return 0;
}
有问题,因为它输出0 0 0
而不是0 30 0
...
但是,如果我像这样稍微改变它:
#include <iostream>
#include <boost/tokenizer.hpp>
using namespace std;
int main()
{
typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
string s ("(0,30,0)");
//Modified lines:
string t (s.substr(1,s.size()-2));
tokenizer tokens(t, boost::char_separator<char>(","));
for (tokenizer::iterator tok_iter = tokens.begin(); tok_iter != tokens.end(); ++tok_iter)
cout<<*tok_iter<<endl;
return 0;
}
它工作正常!这怎么可能 ?乍一看,我会认为这两个版本是一样的......