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


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


std::find_first_of用於比較兩個容器之間的元素。它將[first1,last1)範圍內的所有元素與[first2,last2)範圍內的元素進行比較,如果在第一個範圍內找到第二個範圍內的任何元素,則返回一個迭代器元件。

如果在兩個範圍內有一個以上公共元素,則返回第一個容器中存在的第一個公共元素的迭代器。如果沒有匹配項,則返回指向last1的迭代器。

可以按以下兩種方式使用它:


  1. 使用==比較元素:

    用法:

    Template
    ForwardIterator1 find_first_of(ForwardIterator1 first1,
                                   ForwardIterator1 last1,
                                   ForwardIterator2 first2, 
                                   ForwardIterator2 last2);
    
    first1: Forward iterator to the first element 
           in the first range.
    last1: Forward iterator to the last element 
           in the first range.
    first2: Forward iterator to the first element
           in the second range.
    last2: Forward iterator to the last element 
           in the second range.
    
    返回值:It returns an iterator to the 
                  first element in [first1,last1) that is part of 
                  [first2,last2). If no matches are found or [first2,
                  last2) is empty, the function returns last1.
    
    // C++ program to demonstrate  
    // the use of std::find_first_of 
    #include<iostream> 
    #include<vector> 
    #include<algorithm> 
    using namespace std; 
    int main() 
    { 
        // Defining first container 
        vector<int>v = {1, 3, 3, 3, 10, 1, 3, 3, 7, 7, 8} , i; 
      
        // Defining second container 
        vector<int>v1 = {1, 3, 10}; 
      
        vector<int>::iterator ip; 
          
        // Using std::find_first_of 
        ip = std::find_first_of(v.begin(), v.end(), v1.begin(), 
                                v1.end()); 
      
        // Displaying the first common element found 
        cout << *ip << "\n"; 
      
        // Finding the second common element 
        ip = std::find_first_of(ip + 1, v.end(), v1.begin(), 
                                v1.end()); 
      
        // Displaying the second common element found 
        cout << *ip << "\n"; 
          
        return 0; 
    }

    輸出:

    1 
    3
    

    在這兩個向量中,第一個公共元素均為1,要找到第二個公共元素,我們將第一個範圍的起點作為已找到的第一個公共元素旁邊的元素。

  2. 通過使用預定義函數進行比較:

    用法:

    Template
       ForwardIterator1 find_first_of (ForwardIterator1 first1, 
                                       ForwardIterator1 last1,
                                       ForwardIterator2 first2, 
                                       ForwardIterator2 last2,
                                       BinaryPredicate pred);
    
    Here, first1, last1, first2 and last2 are the same as
    previous case.
    
    Pred: Binary function that accepts two 
    elements as arguments (one of each of the two sequences, 
    in the same order), and returns a value convertible to 
    bool. The value returned 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.
    
    
    返回值:It returns an iterator to 
    the first element in [first1,last1) that is part of 
    [first2,last2). If no matches are found or [first2,last2) 
    is empty, the function returns last1.
    // C++ program to demonstrate 
    // the use of std::find_first_of 
    #include<iostream> 
    #include<vector> 
    #include<algorithm> 
    using namespace std; 
      
    // Defining the BinaryFunction 
    bool Pred (int a, int b) 
    { 
        if ( a % b == 0) { 
            return 1; 
        } else { 
            return 0; 
        } 
    } 
    int main() 
    { 
        // Defining first container 
        vector<int>v = {1, 5, 7, 11, 13, 15, 30, 30, 7} , i; 
      
        // Defining second container 
        vector<int>v1 = {2, 3, 4}; 
      
        vector<int>::iterator ip; 
          
        // Using std::find_first_of 
        ip = std::find_first_of(v.begin(), v.end(), v1.begin(), 
                                v1.end(), Pred); 
      
        // Displaying the first element satisfying Pred() 
        cout << *ip << "\n"; 
      
        return 0; 
    }

    輸出:

    15
    

    在這裏,我們以一種方式操縱二進製函數,以試圖在第一個容器中查找第一個數字,該數字是第二個容器中任何數字的倍數。在這種情況下,第15個是第一個,因為它可以被3整除。

可能的應用:std::find_first_of可用於查找另一個容器中存在的任何元素的第一個匹配項。

  1. 一個可能的應用是在句子中找到第一個元音。
    // C++ program to demonstrate the use of std::find_first_of 
    #include<iostream> 
    #include<vector> 
    #include<string> 
    #include<algorithm> 
    using namespace std; 
    int main() 
    { 
        // Defining first container 
        string s1 = "You are reading about std::find_first_of"; 
      
        // Defining second container containing list of vowels 
        string s2 = {'a', 'A', 'e', 'E', 'i', 'I', 'o', 'O',  
                    'u', 'U'}; 
          
        // Using std::find_first_of to find first occurrence of 
        // a vowel 
        auto ip = std::find_first_of(s1.begin(),s1.end(), 
                                     s2.begin(), s2.end()); 
      
        // Displaying the first vowel found 
        cout << "First vowel found at index "<< (ip - s1.begin())  
             << "\n"; 
        return 0; 
    }

    輸出:

    First vowel found at index 1
    

    說明:std::find_first_of在第一個容器中的第二個容器中搜索任何元素的第一個匹配項,並以此方式找出句子中存在的第一個元音。

  2. 它也可以用來查找列表中出現的第一個奇數和偶數。
    // C++ program to find the first occurrence of an odd 
    // and even number 
    #include<iostream> 
    #include<vector> 
    #include<algorithm> 
    using namespace std; 
      
    // Defining the Predicate Function to find first occurrence 
    // of an odd number 
    bool pred( int a, int b) 
    { 
        if (a % b != 0) { 
            return 1; 
        } else { 
            return 0; 
        } 
    } 
      
    // Defining the Predicate Function to find first occurrence 
    // of an even number 
    bool pred1( int a, int b) 
    { 
        if (a % b == 0) { 
            return 1; 
        } else { 
            return 0; 
        } 
    } 
      
    int main() 
    { 
      
        // Defining a vector 
        vector<int>v1 = {1, 3, 4, 5, 6, 7, 8, 10}; 
          
        // Declaring a sub-sequence 
        vector<int>v2 = {2}; 
           
        // Using std::find_first_of to find the first  
        // occurrence of an odd number 
        vector<int>::iterator ip; 
        ip = std::find_first_of(v1.begin(), v1.end(), v2.begin(), 
                           v2.end(), pred); 
       
        // Displaying the index where the first odd number  
        // occurred 
        cout << "First odd no. occurs at index " 
             <<  (ip - v1.begin()); 
      
      
        // Using std::find_first_of to find the last occurrence    
        // of an even number 
        ip = std::find_first_of(v1.begin(), v1.end(), v2.begin(), 
                           v2.end(), pred1); 
       
        // Displaying the index where the first even number  
        // occurred 
        cout << "\nFirst even no. occurs at index " 
             <<  (ip - v1.begin()); 
      
        return 0; 
    }

    輸出:

    First odd no. occurs at index 0
    First even no. occurs at index 2
    

    說明:這裏,我們已經將2存儲在一個容器中,並且借助謂詞函數,我們正在第一個容器中尋找第一個數字,該數字不能被2整除(對於奇數),而可以被2整除(對於偶數)數)。

時間複雜度:O(n1 * n2),其中,n1是第一個範圍內的元素數,n2是第二個範圍內的元素數。



相關用法


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