【问题标题】:Recursively get index of substring start in string in C++递归获取C++中字符串开始的子字符串索引
【发布时间】:2015-05-13 16:36:31
【问题描述】:

给定函数:

int getIndex(string mystring, string substring);

我如何找到mystring 的索引,substring 开始的位置(如果有的话)?我不想使用默认的 find 函数,我想自己制作(在 C++ 中)。如果存在该子字符串,我的 find 方法将返回 true。

到目前为止,我有以下想法:

int getIndex(string mystring, string substring)
{
    if(find(mystring, substring))
        return counter();
return -1;
}
int counter()
{
        int count; //I'm not sure how to use this value without a global
                     //variable or variable declared in main()
        ++count;
}

【问题讨论】:

  • 没有理由要从头开始重新实现 std::string 函数。
  • 肯定有,学习实践。
  • 如果你能找到子串,你应该能找到它开始的位置。
  • 你还在用 find
  • @VectorCroc:C++ 编程的世界充满了不使用标准库的非理性原因。如果只是为了学习,那么公平,但是您应该始终确保在您的问题中指定这一点。否则,我会自动进入代码审查状态 :)

标签: c++ string recursion substring


【解决方案1】:

函数可以这样写

std::string::size_type GetIndex( const std::string &myString, 
                                 const std::string &subString )
{
    if ( myString.size() < subString.size() ) return std::string::npos;
    if ( myString.substr( 0, subString.size() ) == subString ) return 0;

    std::string::size_type n = GetIndex( myString.substr( 1 ), subString );

    return ( n == std::string::npos ) ? std::string::npos : n + 1;
}                                 

这是一个演示程序

#include <iostream>
#include <string>

std::string::size_type GetIndex( const std::string &myString, 
                                 const std::string &subString )
{
    if ( myString.size() < subString.size() ) return std::string::npos;
    if ( myString.substr( 0, subString.size() ) == subString ) return 0;

    std::string::size_type n = GetIndex( myString.substr( 1 ), subString );

    return ( n == std::string::npos ) ? std::string::npos : n + 1;
}                                 

int main()
{
    std::string::size_type n = GetIndex( "Hello World", "World" );

    if ( n != std::string::npos )
    {
        std::cout << "The substring is found at position " << n  << std::endl;
    }
    else
    {
        std::cout << "The substring is not found" << std::endl;
    }

    n = GetIndex( "Hello C", "C++" );

    if ( n != std::string::npos )
    {
        std::cout << "The substring is found at position " << n  << std::endl;
    }
    else
    {
        std::cout << "The substring is not found" << std::endl;
    }
}    

它的输出是

The substring is found at position 6
The substring is not found

如果您愿意,可以将std::string::size_type 替换为int,将std::string::npos 替换为-1。:)

【讨论】:

    【解决方案2】:

    创建一个重载函数来完成真正的工作并使其递归。使用起始索引从第一个函数调用第二个函数。

    int getIndex(char const* mystring, size_t len1,
                 char const* substring, size_t len2, int index)
    {
       if ( len1 < len2 )
       {
          return -1;
       }
    
       if ( strncmp(mystring, substring, len2) == 0)
       {
         return index;
       }
    
       return getIndex(mystring+1, len1-1, substring, len2, index+1);   
    }
    
    int getIndex(string mystring, string substring)
    {
       return getIndex(mystring.c_str(), mystring.size(),
                       substring.c_str(), substring.size(),
                       0);
    }
    

    【讨论】:

      【解决方案3】:

      如何迭代字符串,每次字符匹配子字符串的第一个字符时停止,然后查看计数器是否足够小以容纳子字符串,如果是,则在子循环中逐个字符迭代到找到匹配项?

      【讨论】:

      • 如果有人可以发布一些有用的代码。我避免比较字符,因为它很乱而且没有必要。
      • 如果我可以使用字符串作为分隔符,我认为这会有所帮助,但我认为分隔符是为字符保留的。
      • 你不能在没有最终比较字符的情况下比较字符串。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-11-17
      • 1970-01-01
      • 2019-07-22
      • 2023-03-27
      • 2011-11-21
      • 2023-03-11
      • 2023-03-03
      相关资源
      最近更新 更多