Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/130.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 用于反向查找的递归函数_C++_Recursion - Fatal编程技术网

C++ 用于反向查找的递归函数

C++ 用于反向查找的递归函数,c++,recursion,C++,Recursion,char是递归函数。它不应该有任何l递归不使用循环 因此,如果您的函数名为rfind…,那么在rfind中,您必须完成以下三项: 终止而不查找-确定已到达str的开头,然后使用nullptr退出 在str中的某个位置以find-recognize'ch'终止,然后退出并返回一个char* 继续搜索-通过递归调用rfind…,并返回递归调用返回的值 编辑-只是为了好玩,这里有一个3参数的rfind,我认为它更可读 注意:没有循环,两种情况都是退出,还有尾部递归 // find _last_ occ

char是递归函数。它不应该有任何l

递归不使用循环

因此,如果您的函数名为rfind…,那么在rfind中,您必须完成以下三项:

终止而不查找-确定已到达str的开头,然后使用nullptr退出

在str中的某个位置以find-recognize'ch'终止,然后退出并返回一个char*

继续搜索-通过递归调用rfind…,并返回递归调用返回的值

编辑-只是为了好玩,这里有一个3参数的rfind,我认为它更可读

注意:没有循环,两种情况都是退出,还有尾部递归

// find _last_ occurrance of ch in str, starting at indx
char*  rfind(char*  str,  char ch, int indx)
{
   // Req 1: terminate when not found, return nullptr
   if(indx < 0)       return (nullptr);  

   // Req 2: terminate when found, return char*
   if(ch == str[indx]) return(&str[indx]) 

   // Req 3: continue search, return search results
   return ( rfind (str, ch, (indx-1)) );    // keep looking
}
我最近对递归代码的-O3优化印象深刻

在某些尾部递归中,编译器完全删除了递归调用和返回,这是没有发生堆栈溢出的证据,在-O0处编译时,可执行文件总是崩溃

在至少一个可重复的测试中,得到的完全优化的递归实现比相应的完全优化的循环实现快50%


这是一个非常直接的递归实现。可悲的是,这并不是尾部递归

全面实施: 基本大小写:str是由零个字符组成的字符串: 递归调用: 确定我们想要的是字符串其余部分的结果,还是当前位置:
这是一个家庭作业问题,因为除了作为学习递归的练习外,谁会想递归地做呢?如果你不能开始你的家庭作业,你应该向你的老师寻求帮助。他们显然还没有为你提供教育而挣钱。想想你想要停止这种循环的原因。然后考虑如何在每次递归调用期间访问每个字符。听起来你的教授要求你实现strrchr。嗨,我对这个递归有点陌生,但在你的代码中,它是指向ch的最后一次出现还是第一次出现?根据我的问题,它应该指向最后一个事件。不过还是非常感谢@AnitejRao很容易验证。p-s+1将告诉您返回的字符的位置。我的代码返回指向str中ch的最后一个实例的指针。如果找不到ch,则返回NULL。@AnitejRao-hint 1-开始搜索之前,前3个步骤旋转到行尾的0。
int t122()
{
    char str[] = "t123abcdefg*o4";

    size_t strSize = strlen(str);
    std::cout << "\n       strSize = " << strSize << "            " << (void*)str
              << "\n       01234567890123" << std::endl;

    for (size_t i = 0; i < strSize; ++i) {
       test_rfind(str, str[i]);
    }
    test_rfind(str, 'z');
    test_rfind(str, '0');  // digit '0'
    test_rfind(str, 'K');
    // ...
 }
void test_rfind(char* str, char tgt)
{
   do // not part of the recursion, not really a loop, just a test simplification
   {
      if (0 == str) { std::cout << "       str is null " << std::endl;  break; }

      // ===================================================================

      char* pos = rfind(str, tgt);  // 2 parameter invocation - see below

      // ===================================================================

      if (nullptr == pos) {
         std::cout << "rfind('" << std::setw(14) << str
                << "', '" << tgt << "') :           "
                   << "  char '" << tgt
                   << "' not found" << std::endl;
         break;
      }

      // else found
      std::cout << "rfind('" << std::setw(14) << str
                << "', '" << tgt << "') = "
                << (void*)pos
                << std::setw(20) << pos
                << "    last '"  << pos[0]
                << "' at indx: " << (pos - str) << std::endl;
   }while(0);
}
// two parameter
char*  rfind(char*  str,  char tgt)
{
   // pre-validation
   if (0 == str) return(nullptr); // one null check here, rather than 'inside' recursion

   // pre-validation - tbr: check range (0 <= char <= 127)
   // allow (0 == tgt): a char can be 0

   // now use the 'just for fun' 3 parameter rfind
   return ( rfind(str, tgt, strlen(str)) );  // use tail recursion
}
       strSize = 14            0xbff8f4bd
       01234567890123
rfind('t123abcdefg*o4', 't') = 0xbff8f4bd      t123abcdefg*o4    last 't' at indx: 0
rfind('t123abcdefg*o4', '1') = 0xbff8f4be       123abcdefg*o4    last '1' at indx: 1
rfind('t123abcdefg*o4', '2') = 0xbff8f4bf        23abcdefg*o4    last '2' at indx: 2
rfind('t123abcdefg*o4', '3') = 0xbff8f4c0         3abcdefg*o4    last '3' at indx: 3
rfind('t123abcdefg*o4', 'a') = 0xbff8f4c1          abcdefg*o4    last 'a' at indx: 4
rfind('t123abcdefg*o4', 'b') = 0xbff8f4c2           bcdefg*o4    last 'b' at indx: 5
rfind('t123abcdefg*o4', 'c') = 0xbff8f4c3            cdefg*o4    last 'c' at indx: 6
rfind('t123abcdefg*o4', 'd') = 0xbff8f4c4             defg*o4    last 'd' at indx: 7
rfind('t123abcdefg*o4', 'e') = 0xbff8f4c5              efg*o4    last 'e' at indx: 8
rfind('t123abcdefg*o4', 'f') = 0xbff8f4c6               fg*o4    last 'f' at indx: 9
rfind('t123abcdefg*o4', 'g') = 0xbff8f4c7                g*o4    last 'g' at indx: 10
rfind('t123abcdefg*o4', '*') = 0xbff8f4c8                 *o4    last '*' at indx: 11
rfind('t123abcdefg*o4', 'o') = 0xbff8f4c9                  o4    last 'o' at indx: 12
rfind('t123abcdefg*o4', '4') = 0xbff8f4ca                   4    last '4' at indx: 13
rfind('t123abcdefg*o4', 'z') :             char 'z' not found
rfind('t123abcdefg*o4', '0') :             char '0' not found
rfind('t123abcdefg*o4', 'K') :             char 'K' not found
char *rfind(char* str, char ch) { 
     if (*str == '\0')
         return NULL; 
     char * pos = rfind(str + 1, ch);
     if (pos != NULL)
         return pos;
     if (*str == ch)
         return str;
     return NULL;
}
     if (*str == '\0')
         return NULL; 
     char * pos = rfind(str + 1, ch);
     if (pos != NULL)
         return pos;
     if (*str == ch)
         return str;
     return NULL;