递归返回函数,在特殊情况下不返回
Recursion returning function without returning in special cases
我需要通过蛮力组合给定的密码。所以我决定使用递归函数来返回string
string bruteforce(string password, string forcedPassword)
{
if (password.length() == forcedPassword.length())
{
if (password == forcedPassword)
{
return forcedPassword;
}
// What can I do here to return nothing and continue from the previous step?
}
for (int j = 32; j <= 126; j++)
{
forcedPassword += char(j);
bruteforce(password, forcedPassword);
}
}
int main()
{
...
cin >> password;
cout << bruteforce(password, "");
...
}
问题是当我得到password.length() == forcedPassword.length()
但它们不一样时。我只需要退出递归的最后一步,没有任何返回值。有什么办法吗?
假设密码不为空(否则使用optional
(,你可以写:
std::string bruteforce(const std::string& password, const std::string& forcedPassword)
{
if (password.length() == forcedPassword.length())
{
if (password == forcedPassword)
{
return forcedPassword;
}
return "";
}
for (int j = 32; j <= 126; j++)
{
auto res = bruteforce(password, forcedPassword + char(j));
if (!res.empty()) {
return res;
}
}
return "";
}
我不知道
这在您的特定情况下是否会对您有所帮助:
std::string bruteforce( const std::string& enteredPassword, std::string& forcedPassword ) {
if ( enteredPassword.length() == forcedPassword.length() ) {
if ( enteredPassword == forcedPassword ) {
return forcedPassword;
}
return "";
}
static int j = 31;
while ( j <= 126 ) {
++j;
// Display forcedPassword to test it for each recursive iteration
std::cout << forcedPassword << "n";
return bruteforce( enteredPassword, forcedPassword + char( j ) );
}
return "";
}
int main() {
std::string password;
std::string forcedPassword( "Hello" );
std::cout << "Enter the passwordn";
std::cin >> password;
std::cout << bruteforce( password, forcedPassword );
std::cout << "Press any key and enter to quit.n";
char q;
std::cin >> q;
return 0;
}
但这确实可以编译、构建和运行,没有错误,并且这里没有堆栈溢出。
我使enteredPassword
成为const ref
,因为它不会被此函数修改。对于forcedPassword
,由于您正在显示您正在连接到此字符串,因此我选择将其设置为将要修改的non const reference
。我选择使用 while 循环而不是来自[32,126]
的 for 循环,而是使用static int
计数器。我最初将此值设置为 31
,并在<= 126
时检查此循环。然后在循环中,我pre-increment
static
计数器,然后显示forcedPassword
值以进行调试。最后,我返回这个递归函数,将原始未修改的enteredPassword
与forcedPassword
一起传递,同时使用 char(j)
更新它。对于有no operations to be made
的控制路径,我只是简单地返回"";
按照评论中的建议使用布尔函数,您可以这样做
#include <iostream>
using namespace std;
bool bruteforce(const string& password, string& forcedPassword, unsigned int length=0)
{
if (password.length() == length)
{
if (password == forcedPassword)
{
return true;
}
return false;
}
for (int j = 32; j <= 126; ++j)
{
forcedPassword[length] = char(j);
if ((bruteforce(password, forcedPassword, length+1)))
return true;
}
}
std::string findpassword(const string& password) {
std::string forcedPassword = "";
while (forcedPassword.length() < password.length())
forcedPassword.push_back('a');
if (bruteforce(password, forcedPassword))
return forcedPassword;
else
return "failed";
}
int main()
{
std::string password;
cin >> password;
cout << findpassword(password);
}
相关文章:
- G++ 编译器是否在未使用返回值的情况下将 constexpr 函数视为常规函数?
- 如何在没有返回值的情况下使用(特征)unaryExpr 和 lambda 函数?
- C++箭头运算符 (->) 是否在所有情况下都返回左值?
- 在这种特殊情况下,我是否需要在共享内存中使用原子类型
- UDP 套接字 select() 在某些情况下无延迟(超时)返回 1
- 为什么在某些情况下从函数返回类型中删除 cv 限定符?
- 如果在开关情况下不匹配,为什么此函数返回 ASCII 值?
- 在这种情况下,如何返回对数组中元素的引用?
- 为什么在这种特殊情况下不需要将 std::hash() 的专用化注入到 std 命名空间中?
- 在开关情况下返回布尔值
- 如何更改此空隙函数,以便在特殊条件下返回一个生成的字符串
- 在这种情况下,为什么使用 *此必需的对象引用返回对象引用
- 在特殊情况下使析构函数不是虚拟的,并删除基指针是否安全
- 递归返回函数,在特殊情况下不返回
- 为什么GetEffectiveRightsFromAcl在这种情况下不返回任何权利?
- 为什么在这种特殊情况下我不需要模板参数?
- 为什么在这种情况下"std::begin()"总是返回"const_iterator"?
- 在开关情况下可以默认返回 cin
- 在特殊情况下可以忽略[[nodiscard]]吗
- 在特殊情况下:是否&比%快?