我想比较两个字符串字符使用我创建的bool match(string,string)
字符,我相信它比较正确,当我输入两个字符串不相等,它会输出false!但是当我检查布尔时,它没有返回false。 我想不出这种行为的原因,我希望有人能帮助我。 代码:奇怪布尔returing行为
#include <iostream>
#include <cassert>
#include <cmath>
#include <fstream>
#include <vector>
using namespace std;
bool match(string pattern, string source)
{
if(pattern.size() == 0&& source.size() == 0)
{
return true;
}
else if(pattern[0] == source[0])
{
pattern.erase(0,1);
source.erase(0,1);
match(pattern,source);
}
else
{
cout << "false" << endl;
return false;
}
}
int main()
{
string test1 = "hballo";
string test2 = "hallo";
bool match_found = match(test1,test2);
if(match_found)
{
cout << "match found!"<< endl;
}
else if(!match_found)
{
cout << "match not found!"<< endl;
}
}
谢谢你的回答NPE!即使它不是最快的答案,我仍然感谢你的回答! –
除此之外,'pattern [0] == source [0]'部分也被破坏了。此时'pattern'或'source'可以为空。 – melpomene
@melpomene:好了,谢谢! – NPE