#include <iostream>
#include<string>
bool findG( const std::string name)
{
return name.length() >= 3 && name[0] == 'H';
}
bool NotfindG( const std::string name)
{
return !findG(name);
}
int main()
{
std::string name = "agHello";
if(findG(name))
{
std::cout << "It found Hello\n";
}
else
{
std::cout << "It did not find hello \n";
}
}
您看到的是布尔函数,如果找到参数中给定的字符串,该函数将返回。
我了解该功能在做什么。我的兴趣是了解上面代码中函数NotfindG
的活动?
bool NotfindG( const std::string name)
{
return !findG(name);
}
[我看到有人在使用它,但是对我来说,即使没有布尔函数NotfindG
,该函数也应该可以工作(我是在else条件下)。您能给我一些为什么有人会使用的理由吗?
在您的示例代码中,没有任何实际调用NotFindG
,因此确实不需要它。
bool函数的通用Not*
变体用途有限,但我可以提出一些理由:
FindG
看起来特定于某种业务逻辑,这意味着尽可能多地包装可能是个好主意。也许NotFindG
是一个特定的要求,理论上可能不是FindG
,因此从技术上讲,比NotFindG
调用!FindG
更准确。遇到这种情况,也许FindG
是应该删除的那个。