2014-09-18 67 views
0

我寫了一個小方法來查看一個字符串是否包含另一個字符串。 雖然我只有一個小問題,但它總是返回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

回答

1

的問題是在這裏

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

您應該0needleChars.size(),也許ii + needleChars.size()之間循環。以下if聲明也需要進行調整; needlehaystack的正確數組索引不會相同。

+0

謝謝你的回答。我多次查看它,但沒有注意到我的錯誤。我會盡快結束這個問題。 – 2014-09-18 16:05:23

1

幾分錢,隨心所欲。

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