C++ - Haystack/Needle字符串检查总是返回false

问题描述:

我写了一个小方法来查看一个字符串是否包含另一个字符串。 虽然我只有一个小问题,但它总是返回false。C++ - Haystack/Needle字符串检查总是返回false

鉴于haystack是一个名为salaryCheck的字符串,值为“10.1”,针为“。”。它总是返回false。

根据我的理解,它应该返回true,我首先将所有内容都放入字符向量中以供读取。然后我进入一个循环,在这个循环中,我检查第一针字符是否匹配干草堆[i]。 如果第一针字符与haystack [i]匹配,我继续进入另一个循环,在那里我将haystack [i]开始的所有干草堆与完整的针字符列表进行比较。

据我所知,这应该返回true与我给出的参数。

这里是我的代码:

bool contains(std::string& haystack, std::string needle){ 
    if (haystack.size() < needle.size()) 
     return false; 

    bool returnValue = false; 
    std::vector<char> haystackChars; 
    std::vector<char> needleChars; 

    for (char c : haystack) 
     haystackChars.push_back(c); 
    for (char c : needle) 
     needleChars.push_back(c); 

    for (int i = 0; i < haystackChars.size() && !returnValue; i++){ 
     if (needleChars[0] == haystackChars[i]) 
      for (int i2 = i; i2 < needleChars.size(); i2++){ 
       if (needleChars[i2] == haystackChars[i2]) 
        returnValue = true; 
       else{ 
        returnValue = false; 
        break; 
       } 
      } 
    } 

    return returnValue; 
} 
+1

你为什么要编写自己的函数?为什么不'string :: find'或['search'](http://en.cppreference.com/w/cpp/algorithm/search)? – dyp 2014-09-18 15:57:10

+5

为什么你通过(非const)引用传递一个字符串,而另一个值?你为什么要将他们的角色复制到矢量中?你为什么使用'for'循环来做到这一点?还有更多的问题......我建议你在[CodeReview](http://codereview.stackexchange.com)上发布这段代码,以便获得有关可以改进的一些技巧。 – dyp 2014-09-18 15:58:45

+0

这似乎是一个很好的学习机会。 – 2014-09-18 15:59:08

的问题是在这里

 for (int i2 = i; i2 < needleChars.size(); i2++){ 

您应该0needleChars.size(),也许ii + needleChars.size()之间循环。以下if声明也需要进行调整; needlehaystack的正确数组索引不会相同。

+0

谢谢你的回答。我多次查看它,但没有注意到我的错误。我会尽快结束这个问题。 – 2014-09-18 16:05:23

几分钱,随心所欲。

bool contains(std::string& haystack, std::string needle){ 
// you only need to inspect haystack and needle, so 
bool contains(const std::string& haystack, const std::string& needle) 
// is preferred. You also get convinient constructors, simplifying the 
// function call. And of course the const guarantee 

// This is ok. 
    if (haystack.size() < needle.size()) 
     return false; 
// returnValue will be unneccesary, you can leave as soon as you find the positive. 
    bool returnValue = false; 
// The vector does not add anything useful. std::string works fine with indexes. 
    std::vector<char> haystackChars; 
    std::vector<char> needleChars; 

    for (char c : haystack) 
     haystackChars.push_back(c); 
    for (char c : needle) 
     needleChars.push_back(c); 

    // The algorithm is unnecessarily complex, 
    // use the string members to simplify. 
    for (int i = 0; i < haystackChars.size() && !returnValue; i++){ 
     if (needleChars[0] == haystackChars[i]) 
      // This for statment can be rewritten as 
      if (haystack.substring(i, needleSize) == needle) 
       return true; 
      for (int i2 = i; i2 < needleChars.size(); i2++){ 
       if (needleChars[i2] == haystackChars[i2]) 
        returnValue = true; 
       else{ 
        returnValue = false; 
        break; 
       } 
      } 
    } 
    // this becomes return false. 
    return returnValue; 
} 

因此,与这些修订:

bool contains(const std::string& haystack, const std::string& needle){ 
    if (haystack.size() < needle.size()) 
     return false; 
    size_t needleSize = needle.size(); 
    for (int i = 0; i < haystack.size(); i++){ 
     if (haystack.substr(i, needleSize) == needle) 
       return true; 
    } 
    return false; 
} 

作为DYP笔记,SUBSTR可能是潜在成本高昂,串::找救援。有条件的也可以写成

 if (haystack.find(needle.c_str(), i, needleSize) == i) 
       return true; 

当然还是所建议的,如果这不是演习,这将完成为

if (haystack.find(needle) != string::npos) 
+0

@dyp大错。感谢您的高举。 – 2014-09-18 17:09:36

+0

好多了;)但我认为没有必要使用'needle.c_str()','string :: find'有一个重载,它需要一个'string const&'。无论如何,我认为'== 0'比较不正确:'find'返回一个位置(和索引)。 – dyp 2014-09-18 17:14:54

+0

@dyp =)再次感谢。其中一天,我将学习检查我的代码。 – 2014-09-18 17:20:09