我的代码c++报错了,好像是运行错误,很奇怪,这是为啥
#include<iostream>
#include<string>
using namespace std;
struct tre{
int l,r;
char d;
}a[50];
int top=0;
void t(string x,string y,char z,int b)
{
a[++top].d=x[0];
if(z=='l') a[b].l=top;
if(z=='r') a[b].r=top;
int c=y.find(x[0]);
t(x.substr(1,c),y.substr(0,c-1),'l',top);
t(x.substr(c+1,x.size()-c),y.substr(c+1,y.size()-c),'r',top);
}
void out(int b)
{
if(!b) return;
out(a[b].l);
out(a[b].r);
cout<<a[b].d;
}
int main()
{
string _1,_2;
cin>>_1>>_2;
t(_1,_2,'n',0);
out(1);
return 0;
}
报错信息:
terminate called after throwing an instance of 'std::out_of_range'
what(): basic_string::substr: __pos (which is 1) > this->size() (which is 0)