Searches for the last occurrence of a subrang

View versions (1)

Definition

The find_end() algorithm is defined in the standard header <algorithm> and in the nonstandard backward-compatibility header <algo.h>.

Interface

#include <algorithm>
template < class ForwardIterator1, class ForwardIterator2 >
   ForwardIterator1 find_end(
      ForwardIterator1 first1, 
      ForwardIterator1 last1,
      ForwardIterator2 first2, 
      ForwardIterator2 last2
   );
template < class ForwardIterator1, class ForwardIterator2, class Pr >
   ForwardIterator1 find_end(
      ForwardIterator1 first1, 
      ForwardIterator1 last1,
      ForwardIterator2 first2, 
      ForwardIterator2 last2,
      BinaryPredicate comp
   );

Parameters:

ParameterDescription
first1A forward iterator addressing the position of the first element in the range to be searched
last1A forward iterator addressing the position one past the final element in the range to be searched
first2A forward iterator addressing the position of the first element in the range to be searched
last2A forward iterator addressing the position one past the final element in the range to be searched
compUser-defined predicate function object that defines the condition to be satisfied if two elements are to be taken as equivalent. A binary predicate takes two arguments and returns true when satisfied and false when not satisfied

Description

Find_end finds the last subsequence (elements between first2 and last2) contained into a sequence (elements between first1 and last1 ).

Return Value

Returns a forward iterator addressing the position of the first element of the last subsequence that matches the specified sequence or that is equivalent in a sense specified by a binary predicate.

Complexity

The complexity is linear, find_end performs at most (last1 - first1) * (last2 - first2) comparisons.

References

Example 1
Problem

This program illustrates the use of the STL find_end() algorithm (default version) to find the last occurrence of one range of integer values in a vector within another range of integer values, also in a vector.

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

using namespace std;

int main()
{
  int a1[] = {1, 2, 3, 4, 5, 6, 7, 8, 3, 4, 5, 9, 10, 11};
  vector<int> v1(a1, a1+14);
  cout <<"\nHere are the contents of v1:\n";
  for (vector<int>::size_type i=0; i<v1.size(); i++)
    cout <<v1.at(i)<<" ";

  int a2[] = {3, 4, 5};
  vector<int> v2(a2, a2+3);
  cout <<"\nHere are the contents of v2:\n";
  for (vector<int>::size_type i=0; i<v2.size(); i++)
    cout <<v2.at(i)<<" ";

  vector<int>::iterator p;
  
  p = find_end(v1.begin(), v1.end(), v2.begin(), v2.end());
  if (p != v1.end())
    cout <<"\nThe last instance of v2 in v1 begins at location "
         <<(int)(p-v1.begin()+1)<<".";
  else
    cout <<"\nNo instance of v2 was found in v1.";

  return 0;
}
Solution

Output:

Here are the contents of v1:
1 2 3 4 5 6 7 8 3 4 5 9 10 11

Here are the contents of v2:
3 4 5

The last instance of v2 in v1 begins at location 9.

See Also