如何将此代码转换为使用字符串 [英] How to convert this code to use string
本文介绍了如何将此代码转换为使用字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
char * recursivecombo(char *str, int choices, int level)
{
int len = strlen(str);
level++;
if( level == choices)
{
for (int i = 0; i < len -2; i++)
{
printf("%c", str[i]) ;
}
}
else
{
for (int i = 0; i < len - 2; i++)
{
printf("%c",str[i]);
recursivecombo(str.substr(1), level);
}
}
}
使用字符串而不是char *。
I want to use string instead of char*.
推荐答案
std::string recursivecombo(const std::string& str, int choices, int level)
{
level++;
for (int i = 0; i < str.length() -2; ++i)
{
cout<<str.at(i) ;
if( level != choices)
recursivecombo(str.substr(1),8,/*Missing choce*/ level);
}
/*Missing return value*/
}
这只是一个使用字符串的模拟。您的函数有一些问题
This is just a mock-up using a string. Some issues with your function
1)您的返回值在哪里
1)Where is your return value
2)如果您打算使用字符串如果是C ++,则使用cout而不是printf
2)If you intend to use string use cout, rather than printf, if it is C++
3)使用前缀++。
这篇关于如何将此代码转换为使用字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文