我正在尝试使用bool match(string,string)
我创建的逐个字符比较两个字符串,我相信当我输入两个不相等的字符串时它会正确比较它确实输出假!但是当我检查 bool 时,它没有返回 false。我想不出这种行为的原因,我希望有人能帮助我。编码:
#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;
}
}