我有一个解析过的c ++程序的文本文件,如下所示(将所有行视为.txt文件的内容)
element
unit
element
cpp:include
pcdata
#
element
cpp:directive
pcdata
include
element
cpp:file
pcdata
<iostream>
element
using
pcdata
using
element
namespace
pcdata
namespace
element
name
pcdata
std
pcdata
;
element
class
pcdata
class
element
name
pcdata
Person
element
block
pcdata
{
element
private
element
public
pcdata
public:
element
decl_stmt
element
decl
element
type
element
name
pcdata
string
element
name
pcdata
profession
pcdata
;
element
decl_stmt
element
decl
element
type
element
name
pcdata
int
element
name
pcdata
age
pcdata
;
element
constructor
element
name
pcdata
Person
element
parameter_list
pcdata
()
element
member_init_list
pcdata
:
element
call
element
name
pcdata
profession
element
argument_list
pcdata
(
element
argument
element
expr
element
literal
pcdata
......
......
使用srcml工具解析此文件并删除xml标记。
1-i需要逐行读取文件(或者逐字逐句对我来说)并将其与我的代码中定义的字符串值进行比较。例如,检查单词模式,如果从文件中读取的任何单词是“element”,然后检查下一个单词“class”再次检查下一个单词是否如果下一个单词“class”,如果下一个单词“element”,如果下一个单词是“name”,则再次“pcdata” strong>再次如果下一个单词是“pcdata”然后保存从文件中读取的下一个单词,即在这种情况下“person”(这是第29到36行)矢量/阵列。
2-在一个区块内执行步骤1,即如果读取值为“{”从文件中找到类似于第1点中提到的模式,直到“}”发生保存该块并继续。
我的headder文件是xmlVector.h as
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
class xmlVector
{
public:
void browseFile(); // Read file and load it to vector
void printVector(); // Print loaded vector
typedef vector < string > vector_str; // Define vector to store the File.
vector<string>::iterator vec_it; // used in traversal
public:
string line;
string class_name;
string &temp;
};
class classList : public xmlVector
{
public:
string list_classes();
};
我的xmlVector.cpp是
#include "xmlVector.h"
using namespace std;
void xmlVector::browseFile() // Create vector from parsed data
{
vector_str vect;
ifstream code_File ("Test.txt", ios::in);
while (getline(code_File, line))
{
vect.push_back(line); // add the line to the vector
}
}
void xmlVector::printVector() // Print values in vector_str
{
for (string s : vect)
{
cout << s << endl;
}
}
string classList::list_classes()
{
for (str_it = vector_str.begin(); str_it !=vector_str.end(); str_it++)
{
temp = &str_it;
string element1 = "element";
if(*temp.compare(element1) == 0){
str_it = begin()+1;
temp = &str_it;
string element2 = "class";
if(*temp.compare(element2) == 0){
str_it = begin()+2;
temp = &str_it;
string element3 = "pcdata";
if(*temp.compare(element3) == 0){
str_it = begin()+3;
temp = &str_it;
string element4 = "class";
if(*temp.compare(element4) == 0){
str_it = begin()+4;
temp = &str_it;
string element5 = "element";
if(*temp.compare(element5) == 0){
str_it = begin()+5;
temp = &str_it;
string element6 = "name";
if(*temp.compare(element6) == 0){
str_it = begin()+6;
temp = &str_it;
string element7 = "pcdata";
if(*temp.compare(element7) == 0){
class_name = *temp;
cout << "class name = " << class_name;
}
}
}
}
}
}
}
}
}
我的main.cpp是
include <iostream> // Std Library
#include "xmlVector.h"
#include <string>
using namespace std;
int main()
{
xmlVector readXmlVector;
readXmlVector.browseFile();
readXmlVector.printVector();
//cout << "Printed Successfully in the file " << endl;
classList classes;
classes.list_classes();
}
我需要保存输入文件“Test.txt”中存在的所有类及其成员函数的名称。我陷入困境,因为我无法在向量中搜索提到的模式。
请帮助我迭代向量并比较每次迭代中向量中存在的字符串文字,在模式匹配后保存下一个值(可能是向量)。
答案 0 :(得分:0)
使用it
对字符串向量进行迭代,并将参考序列保存在&#39; Sequence
&#39;中。对于给定的职位,请致电if( sequence_starts_here(it, vector.end(), ReferenceSequence) ) return true;
bool
sequence_starts_here(vector<string>::iterator* it,
vector<string>::const_iterator itEnd,
const vector<string>& Sequence)
{
for(vector<string>::const_iterator itStr = Sequence.begin();
itStr != Sequence.end() && it != itEnd && *it == *itStr;
++itStr, ++it);
return itStr == Sequence.end();
}