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


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


先決條件:std::searchstd::search_n是頭文件中定義的STL算法,用於搜索給定元素是否滿足給定的謂詞(如果沒有定義這樣的謂詞則相等)給定的否。與容器元素連續的次數。

它在範圍 [first, last) 中搜索計數元素序列,每個元素比較等於給定值(版本 1)或滿足謂詞(版本 2)。

如果沒有找到這樣的序列,該函數返回一個迭代器到第一個這樣的元素,或一個迭代器到容器的最後一個元素。

std::search_n 的兩個版本定義如下 -

  1. 使用 == 比較元素:

    用法:



     ForwardIterator search_n (ForwardIterator first, ForwardIterator last,
                               Size count, const T& val);
    
    first:
    Forward iterator to beginning of the container to be searched into.
    last:
    Forward iterator to end of the container to be searched into.
    count:
    Minimum number of successive elements to match.
    Size shall be (convertible to) an integral type.
    val:Individual value to be compared.
    返回:
    It returns an iterator to the first element of the sequence.
    If no such sequence is found, the function returns last.
    
    
    // C++ program to demonstrate the use of std::search_n
      
    #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, 3, 3, 6, 7 };
      
        // Declaring the value to be searched for
        int v2 = 3;
      
        // Declaring an iterator for storing the returning pointer
        vector<int>::iterator i1;
      
        // Using std::search_n and storing the result in
        // iterator i1
        i1 = std::search_n(v1.begin(), v1.end(), 2, v2);
      
        // checking if iterator i1 contains end pointer of v1 or not
        if (i1 != v1.end()) {
            cout << "v2 is present consecutively 2 times at index "
                 << (i1 - v1.begin());
        } else {
            cout << "v2 is not present consecutively 2 times in "
                 << "vector v1";
        }
      
        return 0;
    }

    輸出:

    v2 is present consecutively 2 times at index 5
    
  2. 使用謂詞比較元素:
    用法:
    ForwardIterator search_n ( ForwardIterator first, ForwardIterator last,
                               Size count, const T& val, BinaryPredicate pred );
    
    All the arguments are same as previous template, just one more argument is added
    
    pred: Binary function that accepts two arguments 
    (one element from the sequence as first, and val as second),
     and returns a value convertible to bool. The value returned indicates whether 
    the element is considered a 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.
    
    返回:
    It also returns value as per the previous version, i.e., 
    an iterator to the first element of the sequence, 
    satisfying a condition with respect to a given value.
    If no such sequence is found, the function returns last.
    
    // C++ program to demonstrate the use of std::search_n
    // 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, 3, 3, 6, 7 };
      
        // Declaring the value to be compared to v1 based
        // on a given predicate
        int v2 = 3;
      
        // Declaring an iterator for storing the returning pointer
        vector<int>::iterator i1;
      
        // Using std::search_n and storing the result in
        // iterator i1
        i1 = std::search_n(v1.begin(), v1.end(), 2, v2, pred);
      
        // checking if iterator i1 contains end pointer of v1 or not
        if (i1 != v1.end()) {
            cout << "v2 is present consecutively 2 times at index "
                 << (i1 - v1.begin());
        } else {
            cout << "v2 is not present consecutively 2 times "
                 << "in vector v1";
        }
      
        return 0;
    }

    輸出:

    v2 is present consecutively 2 times at index 5
    





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