【问题标题】:String within a string recursive function error字符串递归函数中的字符串错误
【发布时间】:2014-11-11 14:54:02
【问题描述】:

我尝试编写的递归函数有问题。该函数的目的是在一个字符串中找到一个字符串,然后使用递归返回第二个字符串在第一个字符串中所在的索引。

我能够做到这一点。当第二个字符串不包含在第一个字符串中时,就会出现问题。我想告诉用户第二个字符串没有找到。我无法让它转发该消息。

int index_of(string s, string t){
  int len1 = s.length(), len2 = t.length(), index = 0;
  if (len1==len2){
     if (s.substr(index, len2) == t){
       return index;
     }else{
       return -1;
     }
  else{
    index++;
    return index_of(s.substr(index, len1),t)+index;
  }
}

int main(){ 
  string strOne = "", strTwo = "";
  cout << "This program will find the ocurrence of one string within        another.\n\nEnter the string to be searched:\t";
  getline(cin, strOne);
  cout << "\nNow enter the string you want to search for:\t";
  getline(cin, strTwo);
  int index = index_of(strOne, strTwo);
  if (index == -1){
    cout << "\nThe second string cannot be found. Sorry!\n\n";}
  else{
    cout << "\nThe index of the substring is:\t" << index << "\n\n";
  }
  system("PAUSE");
  return 0;
}

任何帮助将不胜感激! :)

【问题讨论】:

  • 您不应该为此目的使用递归函数。在大字符串上,您会出现堆栈溢出。
  • 我不是 C++ 程序员,但你能比较这样的字符串吗:'str1 == str2'?您不必使用 strcmp(str1, str2) 吗?我认为第一个选项只比较字符串的地址?
  • 如果s不包含t,你想让函数做什么?给stdout留言?抛出异常?返回 -1?
  • 2 个问题。 1)调用者将增加-1。 2)长度比较应该是&gt;=而不是==

标签: c++ recursion


【解决方案1】:

您发布的代码中存在许多问题,首先 最重要的是,它不会编译,因为你没有定义 indexindex_of。当然,你只做 当两个字符串长度相同时进行比较。但它是 很难弄清楚您要进行的比较是什么 做,因为您根据未定义的变量获取子字符串 index;如果index0,则没有意义 子字符串,如果 index 不是 0,那么 s.substr( index, len2 ) == t 永远不可能是真的(因为你只有 如果st 的长度相同,则进入此分支。

你真正需要做的是首先用简单的英语定义什么 该功能应该做的:

  • 如果st 短,则无法匹配,所以你 返回 -1。

  • 否则,如果s 的开头等于t,则返回 当前索引。

  • 否则,您将递归 s 的子字符串,删除 第一个字符(并递增index)。

当然,你还需要在某处维护index;在古典 递归,这将作为一个附加的函数参数。

坦率地说,我不会构造所有这些子字符串。远不止如此 在 C++ 中习惯使用迭代器。我会包装递归 非递归函数,因此用户不必通过 在任何额外的参数中:用户可能会调用类似:

int
indexOf( std::string const& text, std::string const& target )
{
    return indexOf( 0, text.begin(), text.end(), target );
}

或者,不传递额外参数:

int
indexOf( std::string const& text, std::string const& target )
{
    std::string::const_iterator results 
            = search( text.begin(), text.end(), target );
    return results == text.end()
        ?  -1
        :  results - text.begin();
}

(我假设这是家庭作业;通常不会使用递归 对于这样的问题。否则,当然,只需致电std::search 在第二个版本中,工作就完成了。或text.find( target ), 它几乎可以返回您想要的。)

【讨论】:

    【解决方案2】:

    如果第一个字符串不包含第二个字符串,index 将无限递增,使string s 长度为零。所以你必须检查第一个字符串是否比第二个短。如果是,则不包含子字符串。

      if (len1 < len2){
        // first string doesn't contain the second
        return -2;
      }else if (len1==len2){
        ...
    

    但是你根本不应该在这里使用递归函数。在string 中还有一个内置函数find: 检查这个问题:Check if a string contains a string in C++

    【讨论】:

    • std::find 查找单个字符。要搜索子字符串,您需要std::search
    • 不完全是,有几个重载:cplusplus.com/reference/string/string/find
    • 但这不是你说的。我对“内置函数”的唯一解释是“标准库函数”,而不是“成员函数”。
    • 是的,通过“内置函数”我提到这个函数已经存在于std::string中。
    猜你喜欢
    • 2020-07-27
    • 2011-03-26
    • 2015-05-12
    • 2019-05-11
    • 2015-05-27
    • 1970-01-01
    • 2016-07-04
    • 2016-04-28
    • 2015-08-16
    相关资源
    最近更新 更多