C++.obj语法分析器面

C++ .obj parser face

本文关键字:分析器 语法分析 语法 obj C++      更新时间:2023-10-16

艰难地将.obj文件的面值传递给向量。

f 5/1/1 1/2/1 4/3/1
f 5/1/1 4/3/1 8/4/1
f 3/5/2 7/6/2 8/7/2

这是我需要存储的,但是

f 5//1 1//1 4//1
f 5//1 4//1 8//1
f 3//2 7//2 8//2

有时候可能是这样,我不知道该怎么解决这个问题,谢谢。

这里有一个使用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"。这个例子也应该适合你的需要。