C++ 递归获取C+中字符串中的子字符串start的索引+;

C++ 递归获取C+中字符串中的子字符串start的索引+;,c++,string,recursion,substring,C++,String,Recursion,Substring,鉴于功能: int getIndex(string mystring, string substring); 如果存在mystring子字符串的索引,如何查找该索引的起始位置?我不想使用默认的find函数,我想创建自己的函数(在C++中)。如果存在该子字符串,则find方法返回true 到目前为止,我有以下想法: int getIndex(string mystring, string substring) { if(find(mystring, substring))

鉴于功能:

int getIndex(string mystring, string substring);
如果存在
mystring
子字符串的索引,如何查找该索引的起始位置?我不想使用默认的
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;
}

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

创建一个重载函数,执行实际工作并使其递归。使用起始索引从第一个函数调用第二个函数

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);
}
int-getIndex(char-const*mystring,size\t len1,
字符常量*子字符串,大小\u t len2,整数索引)
{
if(len1
该函数可按以下方式编写

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;
}                                 

如果您愿意,您可以用type
std::string::size\u type
替换
int
,用
std::string::npos
替换
-1
:)

您没有理由想从头开始重新实现
std::string
函数。当然,这是需要学习和实践的。如果您能找到子字符串,您应该能够找到它开始的位置。您仍在使用find@VectorCrocC++编程的世界里充斥着不合理地使用标准库的人。如果只是为了学习,那就足够了,但你应该确保在你的问题中明确说明这一点。否则,我会自动进入代码审查状态:)如果有人能发布一些代码,这将是有益的。我避免比较字符,因为这很混乱而且没有必要。如果我可以使用字符串作为分隔符,我认为这会有所帮助,但我认为delimeters是为字符保留的。如果不最终比较字符,就无法比较字符串。
#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