当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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++。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。