蒟蒻求助,二叉树基础题,运行报错substr内存读取越界,但是我不知道哪里越界了
查看原帖
蒟蒻求助,二叉树基础题,运行报错substr内存读取越界,但是我不知道哪里越界了
184525
lnhrl楼主2020/8/4 20:03

呃,看了下题解的第二篇,跟着他的思路写了一下,然而报错,于是我把递归函数的结构写的跟他一毛一样,仍然报错,他的就不报错,还请各位巨佬指点是哪里出了问题

代码:

#include<string>
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
string f,m;
void wtf(string f,string m)
{
	if(f.empty())
		return ;
	char ans=f[0]; 
	int root=m.find(ans);
	f.erase(f.begin()); 
	string la=f.substr(0,root);
	string ra=f.substr(root);
	string rb=m.substr(0,root);
	string lb=m.substr(root+1);
	wtf(la,ra);
	wtf(lb,rb);
	cout<<ans;
}
int main()
{
	cin>>m>>f;
	wtf(f,m);
	cout<<endl;
	return 0;
}

这是错误

terminate called after throwing an instance of 'std::out_of_range'
  what():  basic_string::substr: __pos (which is 18446744073709551615) > this->size() (which is 4)

上网查了一下,似乎是substr的内存读取越界,但是我并不知道是哪里越了界,麻烦大家告诉我一下如何改正,感激不尽

2020/8/4 20:03
加载中...