2017-09-19 1 views
-1

私がhereから読むtd::search述語を理解しようとしています。私は便宜のために以下に掲載しました。std :: search述語を理解する

#include <algorithm> 
#include <string> 
#include <cctype> 

// haystack=Hello and needle=World 
/// Try to find in the Haystack the Needle - ignore case 
bool findStringIC(const std::string & strHaystack, const std::string & strNeedle) 
{ 
    auto it = std::search(
    strHaystack.begin(), strHaystack.end(), 
    strNeedle.begin(), strNeedle.end(), 
    [](char ch1, char ch2) { 

     std::cout << ch1 << "?" << ch2 << "\n"; 
    return std::toupper(ch1) == std::toupper(ch2); 
    } 
); 
    return (it != strHaystack.end()); 
} 

本質的に私はそれ(述語)がどのように機能するのか混乱します。干し草が単語Helloであり、針が単語Worldであると仮定する。今、私が観察したものから、針の最初の文字は、干し草の山のすべての文字に比べて取得することである - ので、W比べてしまいますLその後、Eその後、Hに....そう

+0

これと25回の比較は何ですか? –

+0

この述語がどのように機能しているのか、私の理解は間違っていますか? – MistyD

+2

いいえ、それは 'std :: search'の仕組みではありません。 –

答えて

1

http://www.cplusplus.com/reference/algorithm/search/?kw=search

私は含まここにいくつかの公式文書へのリンク。

予測は、2番目のオブジェクトの最初のオカレンスを返します。

など。 "Hello"と "ell"を比較すると、

"H" to "e" -> false 
"e" to "e" -> true continue 
"l" to "l" -> true continue 
"l" to "l" -> true return 
関連する問題