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


C++ std::find_end用法及代码示例


std::find_end用于查找容器内子序列的最后一次出现。它在范围[first1,last1)中搜索由[first2,last2)定义的序列的最后一次出现,然后将迭代器返回到其第一个元素,如果没有发现,则返回last1。

它与std::search相似,在std::search中,我们寻找另一个容器中子序列的第一个出现,而在std::find_end中,我们寻找该子序列的最后一个出现-sequence,如果找到了子序列,则将迭代器返回第一个元素。

可以按以下两种方式使用它:


  1. 使用==比较元素:

    用法:

    Template
       ForwardIterator1 find_end (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 of 
    the last occurrence of [first2,last2) in [first1,last1).
    If the sequence is not found or [first2,last2) is empty,
    the function returns last1.
    
    // C++ program to demonstrate the use of std::find_end 
    #include<iostream> 
    #include<vector> 
    #include<algorithm> 
    using namespace std; 
    int main() 
    { 
        // Defining first container 
        vector<int>v = {1, 3, 10, 3, 10, 1, 3, 3, 10, 7, 8,  
                        1, 3, 10}; 
      
        // Defining second container 
        vector<int>v1 = {1, 3, 10}; 
      
        vector<int>::iterator ip; 
          
        // Using std::find_end 
        ip = std::find_end(v.begin(), v.end(), v1.begin(), 
                           v1.end()); 
      
        // Displaying the index where the last common occurrence  
        // begins 
        cout << (ip - v.begin()) << "\n"; 
        return 0; 
    }

    输出:

    11
    
  2. 通过使用预定义函数进行比较:

    用法:

    Template
       ForwardIterator1 find_end (ForwardIterator1 first1, ForwardIterator1 last1,
                                  ForwardIterator2 first2, ForwardIterator2 last2,
                                  BinaryPredicate pred);
    
    Here, first1, last1, first2, and last2 are
    the same as the 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 of
    the last occurrence of [first2,last2) in [first1,last1).
    If the sequence is not found or [first2,last2) is empty, 
    the function returns last1.
    // C++ program to demonstrate the use of std::find_end 
    #include<iostream> 
    #include<vector> 
    #include<algorithm> 
    using namespace std; 
      
    // Defining the BinaryFunction 
    bool Pred (int a, int b) 
    { 
        return (a == b); 
    } 
    int main() 
    { 
        // Defining first container 
        vector<int>v = {1, 5, 7, 11, 13, 15, 30, 30, 7} , i; 
      
        // Defining second container 
        vector<int>v1 = {13, 15}; 
      
        vector<int>::iterator ip; 
          
        // Using std::find_end 
        ip = std::find_end(v.begin(), v.end(), v1.begin(),  
                           v1.end(), Pred); 
      
        // Displaying the index where the last common occurrence 
        // begins 
        cout << (ip - v.begin()) << "\n"; 
      
        return 0; 
    }

    输出:

    4
    

Where can it be used ?

  1. 从头开始搜索:它是std::search的相反变体,即std::search从列表的开头搜索子序列,以便它可以返回该子序列的第一个匹配项。

    另一方面,如果我们要从列表的末尾搜索子序列,则可以使用std::find_end,这将自动成为容器中任何子序列的最后一次出现。 (如果您正在考虑为什么将其称为std::find_end而不是std::search_end,您并不孤单!)

    因此,如果必须从头开始进行搜索,则可以替换std::search。

    // C++ program to demonstrate the use of std::find_end 
       
    #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, 3, 4, 5 }; 
       
        // Declaring the subsequence to be searched for 
        vector<int> v2 = {3, 4}; 
       
        // Declaring an iterator for storing the returning pointer 
        vector<int>::iterator i1; 
       
        // Using std::search to find the first occurrence of v2 
        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 firstly at index " 
                 << (i1 - v1.begin()); 
        } else { 
            cout << "vector2 is not present in vector1"; 
        } 
      
        // Using std::find_end to find the last occurrence of v2 
        i1 = std::find_end(v1.begin(), v1.end(), v2.begin(),  
                           v2.end()); 
       
        // checking if iterator i1 contains end pointer of v1 or  
        //not 
        if (i1 != v1.end()) { 
            cout << "\nvector2 is present lastly at index " 
                 << (i1 - v1.begin()); 
        } else { 
            cout << "vector2 is not present in vector1"; 
        } 
        return 0; 
    }

    输出:

    vector2 is present firstly at index 2
    vector2 is present lastly at index 7
    
  2. 要查找满足条件的元素的最后一次出现:由于std::find_end从头开始搜索,因此我们可以使用此事实以及操纵BinaryFunction来解决需要我们找到任何事物的最后出现的问题(例如,最后一个奇数,最后一个偶数,等等) )。
    // C++ program to find the last occurrence of an odd 
    // and even number 
    #include<iostream> 
    #include<vector> 
    #include<algorithm> 
    using namespace std; 
      
    // Defining the Predicate Function to find the last 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 the last 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_end to find the last occurrence of an 
        // odd number 
        vector<int>::iterator ip; 
        ip = std::find_end(v1.begin(), v1.end(), v2.begin(), 
                           v2.end(), pred); 
       
        // Displaying the index where the last odd number occurred 
        cout << "Last odd no. occurs at " <<  (ip - v1.begin()); 
      
      
        // Using std::find_end to find the last occurrence of an  
        // even number 
        ip = std::find_end(v1.begin(), v1.end(), v2.begin(), 
                           v2.end(), pred1); 
       
        // Displaying the index where the last even number occurred 
        cout << "\nLast even no. occurs at " <<  (ip - v1.begin()); 
      
        return 0; 
    }

    输出:

    Last odd no. occurs at 5
    Last even no. occurs at 7
    

    代码说明:在这里,我们对Binary Function进行了操纵,以便它搜索第一个容器中的元素是否是第二个容器中的多个元素,并且在第二个容器中我们存储了2,因此借助此函数,我们能够找到最后一次出现的奇数或偶数。

相关文章:



相关用法


注:本文由纯净天空筛选整理自 std::find_end in C++。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。