求解,下面这段代码是可以运行的,不过我把处理的数据换成
(IP (NP (NP (NP (CP (IP (VP (VV ()(NP (NR 法新社)(NR 伦敦) (NT 一日) (NN 电) (NN )) (NN 火星) (NN 探测)))))(NP (NN 计划)))(NP (NN 发言人)))(NP (NR 巴拉特)))(VP (VV 说) (PU ,)(IP (IP (VP (VV 一枚)(IP (VP (LCP (IP (NP (NP (NR 美国))(NP (NN 卫星)))(VP (ADVP (AD 无法))(PP (P 与)(NP (CP (IP (VP (ADVP (AD 已))(VP (VV 登陆)(NP (NN 火星)))))(DEC 的))(NP (NP (NP (NP (NR 欧洲))(ADJP (JJ 小)) (NP (NN 猎犬)))(QP (OD 二)(CLP (M 号))) (NP (NN ()))(NP (NN BEAGLE))) (NP (QP (CD 2))(NP (NN )))) (NP (NN 登陆艇)))) (ADVP (AD 连)) (VP (VV 系))))(LC 后))(ADVP (AD ,)) (VP (VV 有意)(NP (CP (IP (VP (ADVP (AD 从小))(VP (VV 猎犬)(NP (QP (OD 二)(CLP (M 号)))(NP (NN 搜集)(NN 信号))))))(DEC 的))(NP (NN 希望))))))))(PU ,) (VP (NP (NT 如今))(ADVP (AD 只好)) (VP (VV 寄)(VP (VV 望)(PP (P 于)(NP (NP (PN 它的))(NP (NN 母)(NN 船) (NN 火星)) (ADJP (JJ 特)) (NP (NN 快车))))))) (PU .))))
之后就不能运行了,然后报错说vector iterator not dereferencable
#include <iostream>
#include <vector>
#include <string>
#include <iterator>
#include <queue>
#include <iomanip>
#include <cstdlib>
using namespace std;
typedef vector<string>::const_iterator iter;
const int word_length = 10;//Max word length, should be even
class SyntaxTree{
struct Node{
string tag;
vector<Node*> children;
int leaf_num;
Node(string tag,vector<Node*> children,int leaf_num=0){
this->tag = tag;
this->children = children;
this->leaf_num = leaf_num;
}
};
Node * _root;
public:
void parseTree(const vector<string>& vec){
iter be = vec.begin();
_root = parseTree(be);
}
void printTree(){
queue<Node*> que;
que.push(_root);
int level_num = 1;
bool end_flag = true;
while(que.size() != 0 && end_flag){
end_flag = false;
int new_level_num = 0;
while(level_num--){
Node *temp = que.front();
printAuxi(temp->leaf_num,temp->tag);
que.pop();
for(int i=0;i!=temp->children.size();i++){
end_flag = true;
que.push(temp->children[i]);
new_level_num++;
}
if(temp->children.size() == 0){
que.push(new Node(" ",vector<Node*>(),1));//Empty Node for aligning
new_level_num++;
}
}
level_num = new_level_num;
cout<<endl;
cout<<endl;
}
}
private:
void printAuxi(int num,string str){
if(str.size() % 2 == 1)
str += " ";
int pad = (word_length * num - str.size()) / 2;
cout<<setw(pad + str.size())<<str;
cout<<setw(pad)<<" ";
}
Node * parseTree(iter& b){
if(*b == ")" || *b == "]"){
b++;
return NULL;
} else if( *b == "(" || *b == "["){
Node *temp = new Node(*(b+1),vector<Node*>());
b += 2;
Node *child;
while((child = parseTree(b)) != NULL){
temp->children.push_back(child);
temp->leaf_num += child->leaf_num;
}
return temp;
} else{
return new Node(*b++,vector<Node*>(),1);
}
return NULL;//nosense
}
};
vector<string> convert(const string& sytree){
vector<string> ret;
for(string::const_iterator i = sytree.begin();i != sytree.end();i++){
if(*i == ' ' | *i == '\t')
continue;
else if(*i == '(' || *i == ')' || *i == '[' || *i == ']')
ret.push_back(string(i,i+1));
else{
string::const_iterator j = i+1;
for (;j != sytree.end() && *j != ' ' && *j != '\t' && *j != '(' && *j != ')' && *j != '[' && *j != ']';j++)
;
ret.push_back(string(i,j));
i = --j;
}
}
return ret;
}
int main(){
string test = "(S(NP I)(VP(VP (V shot) (NP (Det an) (N elephant)))(PP (P in) (NP (Det my) (N pajamas)))))";
vector<string> syt = convert(test);
SyntaxTree syn;
syn.parseTree(syt);
syn.printTree();
return 0;
}
求帮忙解答一下,我C++几乎是小白啊