📜  C++中的std :: find_end

📅  最后修改于: 2021-05-30 13:01:39             🧑  作者: Mango

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.
    
    Return Value: 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
    #include
    #include
    using namespace std;
    int main()
    {
        // Defining first container
        vectorv = {1, 3, 10, 3, 10, 1, 3, 3, 10, 7, 8, 
                        1, 3, 10};
      
        // Defining second container
        vectorv1 = {1, 3, 10};
      
        vector::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.
    
    
    Return Value: 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
    #include
    #include
    using namespace std;
      
    // Defining the BinaryFunction
    bool Pred (int a, int b)
    {
        return (a == b);
    }
    int main()
    {
        // Defining first container
        vectorv = {1, 5, 7, 11, 13, 15, 30, 30, 7} , i;
      
        // Defining second container
        vectorv1 = {13, 15};
      
        vector::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
    

在哪里可以使用?

  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 
    #include 
    #include 
    using namespace std;
    int main()
    {
        int i, j;
       
        // Declaring the sequence to be searched into
        vector v1 = { 1, 2, 3, 4, 5, 6, 7, 3, 4, 5 };
       
        // Declaring the subsequence to be searched for
        vector v2 = {3, 4};
       
        // Declaring an iterator for storing the returning pointer
        vector::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来解决需要我们找到任何事物的最后一次出现的问题(例如last奇数,最后一个偶数,依此类推)。
    // C++ program to find the last occurrence of an odd
    // and even number
    #include
    #include
    #include
    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
        vectorv1 = {1, 3, 4, 5, 6, 7, 8, 10};
          
        // Declaring a sub-sequence
        vectorv2 = {2};
           
        // Using std::find_end to find the last occurrence of an
        // odd number
        vector::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 函数进行了操纵,以便它搜索第一个容器中的元素是否是第二个容器中的多个元素,并且在第二个容器中我们存储了2,因此借助此功能,我们能够找到最后一次出现的奇数或偶数。

相关文章:

  • std :: search
  • std ::查找
  • std :: find_if,std :: find_if_not
  • std :: nth_element
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”