这是一个使用boost::tokenizer 的示例,我使用stdin 读取输入('f' 之后的所有值),然后将值简单地输出到终端。我相信您可以修改它以从文件中读取并将值放在您需要的地方。
示例 1
#include <iostream>
#include <string>
#include <boost/tokenizer.hpp>
using namespace std;
using namespace boost;
void ParseFace(const string& face);
int main(){
cout << "Input a string: " << endl;
string s;
getline(cin,s);
ParseFace(s);
return 0;
}
void ParseFace(const string& face){
boost::char_separator<char> sep(" /");
boost::tokenizer<boost::char_separator<char> > tokens(face, sep);
for(tokenizer<boost::char_separator<char> >::iterator beg=tokens.begin(); beg!=tokens.end();++beg){
cout << *beg << "\n";
}
}
样本输出:
Input a string:
3/5/2 7/6/2 8/7/2
3
5
2
7
6
2
8
7
2
Input a string:
5//1 1//1 4//1
5
1
1
1
4
1
示例 2
注意boost::char_separator<char> sep(" /"); 这一行,这是将被视为有效分隔符的所有标记的说明符。在您的情况下,将其更改为 boost::char_separator<char> sep("/");(无空格)可能会更方便,然后像这样简单地读取字符串:
#include <iostream>
#include <string>
#include <boost/tokenizer.hpp>
#include <sstream>
using namespace std;
using namespace boost;
void ParseFace(istringstream& _input);
int main(){
cout << "Input a string: " << endl;
string s;
getline(cin,s);
istringstream input(s);
char isFace = 'v';
input >> isFace;
if (!input.fail()){
if (isFace == 'f')
ParseFace(input);
}
return 0;
}
void ParseFace(istringstream& _input){
string nextVal;
_input >> nextVal;
while(!_input.fail()){
cout << "Next set: " << endl;
boost::char_separator<char> sep("/");
boost::tokenizer<boost::char_separator<char> > tokens(nextVal, sep);
for(tokenizer<boost::char_separator<char> >::iterator beg=tokens.begin(); beg!=tokens.end();++beg){
cout << *beg << "\n";
}
_input >> nextVal;
}
}
样本输出:
Input a string:
f 5/1/1 1/2/1 4/3/1
Next set:
5
1
1
Next set:
1
2
1
Next set:
4
3
1
Input a string:
f 5//1 1//1 4//1
Next set:
5
1
Next set:
1
1
Next set:
4
1
在第二个示例中,我使用字符串流从整个输入中读取单个字符串,并使用基本检查来查看第一个字符是否为“f”。这个例子也应该适合你的需要。