當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


C++ std::search用法及代碼示例


std::search在頭文件<algorithm>中定義,用於查找滿足另一個序列的條件(如果未定義此謂詞,則等於)的子序列。

  • 它在序列[first1,last1)中搜索由[first2,last2)定義的子序列的第一個匹配項,然後將一個迭代器返回到該匹配項的第一個元素,如果沒有找到匹配項,則返回last1。
  • 它使用operator ==(版本1)或基於任何給定謂詞(版本2)順序比較兩個範圍內的元素。僅當[first2,last2)的所有元素都為true時,才將[first1,last1)的子序列視為匹配。最後,std::search返回此類事件中的第一個。

可以在兩個版本的任何一個中使用它,如下所示:

  1. 為了使用==比較元素:

    ForwardIterator1 search (ForwardIterator1 first1, ForwardIterator1 last1,
    ForwardIterator2 first2, ForwardIterator2 last2);

    first1:
    Forward iterator to beginning of first container to be searched into.
    last1:
    Forward iterator to end of first container to be searched into.
    first2:
    Forward iterator to the beginning of the subsequence of second container to be searched for.
    last2:
    Forward iterator to the ending of the subsequence of second container to be searched for.



    返回: an iterator to the first element of the f
    irst occurrence of [first2, last2) in [first1, last1), or last1
    if no occurrences are found.

    // C++ program to demonstrate the use of std::search 
      
    #include <iostream> 
    #include <vector> 
    #include <algorithm> 
    using namespace std; 
    int main() 
    { 
        int i, j; 
      
        // Declaring the sequence to be searched into 
        vector<int> v1 = { 1, 2, 3, 4, 5, 6, 7 }; 
      
        // Declaring the subsequence to be searched for 
        vector<int> v2 = { 3, 4, 5 }; 
      
        // Declaring an iterator for storing the returning pointer 
        vector<int>::iterator i1; 
      
        // Using std::search and storing the result in 
        // iterator i1 
        i1 = std::search(v1.begin(), v1.end(), v2.begin(), v2.end()); 
      
        // checking if iterator i1 contains end pointer of v1 or not 
        if (i1 != v1.end()) { 
            cout << "vector2 is present at index " << (i1 - v1.begin()); 
        } else { 
            cout << "vector2 is not present in vector1"; 
        } 
      
        return 0; 
    }

    輸出:

    vector2 is present at index 2
    
  2. 對於基於謂詞(或條件)的比較:

    ForwardIterator1 search (ForwardIterator1 first1, ForwardIterator1 last1,
    ForwardIterator2 first2, ForwardIterator2 last2,
    BinaryPredicate pred);

    All the arguments are same as previous template, just one more argument is added

    pred:Binary function that accepts two elements as arguments (one of each of the two containers, in the same order), and returns a value convertible to bool. The returned value indicates whether the elements are considered to match in the context of this function. The function shall not modify any of its arguments. This can either be a function pointer or a function object.

    返回:an iterator, to the first element of the first occurrence of [first2, last2) satisfying a predicate, in [first1, last1), or last1 if no occurrences are found.

    // C++ program to demonstrate the use of std::search 
    // with binary predicate 
    #include <iostream> 
    #include <vector> 
    #include <algorithm> 
    using namespace std; 
      
    // Defining the BinaryPredicate function 
    bool pred(int i, int j) 
    { 
        if (i > j) { 
            return 1; 
        } else { 
            return 0; 
        } 
    } 
      
    int main() 
    { 
        int i, j; 
      
        // Declaring the sequence to be searched into 
        vector<int> v1 = { 1, 2, 3, 4, 5, 6, 7 }; 
      
        // Declaring the subsequence to be compared to based 
        // on predicate 
        vector<int> v2 = { 3, 4, 5 }; 
      
        // Declaring an iterator for storing the returning pointer 
        vector<int>::iterator i1; 
      
        // Using std::search and storing the result in 
        // iterator i1 based on predicate pred 
        i1 = std::search(v1.begin(), v1.end(), v2.begin(), v2.end(), pred); 
      
        // checking if iterator i1 contains end pointer of v1 or not 
        if (i1 != v1.end()) { 
            cout << "vector1 elements are greater than vector2 starting "
                 << "from position " << (i1 - v1.begin()); 
        } else { 
            cout << "vector1 elements are not greater than vector2 "
                 << "elements consecutively."; 
        } 
      
        return 0; 
    }

    輸出:

    vector1 elements are greater than vector2 starting from position 3
    

相關文章:



相關用法


注:本文由純淨天空篩選整理自 std::search in C++。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。