A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.tutorialspoint.com/cpp_standard_library/cpp_algorithm_find_end_predicate.htm below:

C++ Algorithm Library - find_end() Function

C++ Algorithm Library - find_end() Function Description

The C++ function std::algorithm::find_end() finds the last occurrence of the element. It uses binary predicate for comparison.

Declaration

Following is the declaration for std::algorithm::find_end() function form std::algorithm header.

C++98
template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
ForwardIterator1 find_end(ForwardIterator1 first1, ForwardIterator1 last1,
   ForwardIterator2 first2, ForwardIterator2 last2,BinaryPredicate pred);
Parameters Return value

Returns an iterator to the first element of the last occurrence of (first2,last2) in first1,last1.

Exceptions

Throws exception if either element comparison or an operation on an iterator throws exception.

Please note that invalid parameters cause undefined behavior.

Time complexity

Linear.

Example

The following example shows the usage of std::algorithm::find_end() function.

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

bool binary_pred(int a, int b) {
   return (a==b);
}

int main(void) {
   vector<int> v1 = {1, 2, 1, 2, 1, 2};
   vector<int> v2 = {1, 2};

   auto result = find_end(v1.begin(), v1.end(), v2.begin(), v2.end(), binary_pred);

   if (result != v1.end())
      cout << "Last sequence found at location "
         << distance(v1.begin(), result) << endl;

   v2 = {1, 3};

   result = find_end(v1.begin(), v1.end(), v2.begin(), v2.end(), binary_pred);

   if (result == v1.end())
      cout << "Sequence doesn't present in vector." << endl;

   return 0;
}

Let us compile and run the above program, this will produce the following result −

Last sequence found at location 4
Sequence doesn't present in vector.

algorithm.htm


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4