C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
C++ Algorithm Function find_end ()C++ Algorithm find_end()function searches for the last occurrence of a pattern in the container, or say the last occurrence of a small portion of the sequence in the container. It basically searches the range specified by [first1,last1)for the occurrence of sequence which is defined by [first2,last2). If the occurrence is found, an iterator to the first element is returned, otherwise the last1 is returned. Syntaxtemplate<class ForwardIterator1, classForwardIterator2> ForwardIterator1 find_end(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2); template<class ForwardIterator1, class ForwardIterator2, class BinaryPredicate> ForwardIterator1 find_end(ForwardIterator1 first1,ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred); Parameterfirst1: It is a forward iterator to the first element in the range [first1, last1) where the element itself is included in the range. last1: It is a forward iterator to the last element in the range [first1, last1) where the element itself is not included in the range. first2: It is a forward iterator to the first element in the range [first2, last2) where the element itself is included in the range. last2: It is a forward iterator to the last element in the range [first2, last2) where the element itself is not included in the range. pred: It is a binary function that accepts two elements as arguments and performs the task designed by the function. Return valueThe function returns an iterator to the first element of the last occurrence of [first2,last2)in the range [first1,last1).In case the sequence is not found then the function returns last1 value. Example 1#include <iostream> #include <algorithm> #include <vector> bool newfunction (int m, int n) { return (m==n); } int main () { int newints[] = {1,2,3,4,5,1,2,3,4,5}; std::vector<int> haystack (newints,newints+10); int patt1[] = {1,2,3}; std::vector<int>::iterator ti; ti = std::find_end (haystack.begin(), haystack.end(), patt1, patt1+3); if (ti!=haystack.end()) std::cout << "patt1 last found at position " << (ti-haystack.begin()) << '\n'; int patt2[] = {4,5,1}; ti = std::find_end (haystack.begin(), haystack.end(), patt2, patt2+3, newfunction); if (ti!=haystack.end()) std::cout << "patt2 last found at position " << (ti-haystack.begin()) << '\n'; return 0; } Output: patt1 is last found at the position 5 patt2 is last found at the position 3 Example 2#include<iostream> #include<algorithm> #include<vector> using namespace std; int main() { vector<int>u= {1,3,10,3,10,1,3,3,10,7,8,1,3,10}; vector<int>u1={1,3,10}; vector<int>::iterator ti; ti=std::find_end(u.begin(),u.end(),u1.begin(),u1.end()); cout<<(ti-u.begin())<<"\n"; return 0; } Output: 11 ComplexityThe complexity of the function is specified by count2*(1+count1-count2. Here countX specifies the distance between the firstX and lastX. Data racesObjects in both ranges are accessed. ExceptionsThe function throws an exception if any of the argument throws one.
Next TopicC++ Algorithm find_first_of Function
|