find_end

C/C++ Reference

find_end
Syntax:
  #include <algorithm>
  iterator find_end( iterator start, iterator end, iterator seq_start, iterator seq_end );
  iterator find_end( iterator start, iterator end, iterator seq_start, iterator seq_end, BinPred bp );

The find_end() function searches for the sequence of elements denoted by seq_start and seq_end. If such a sequence if found between start and end, an iterator to the first element of the last found sequence is returned. If no such sequence is found, an iterator pointing to end is returned.

If the binary predicate bp is specified, then it is used to when elements match.

For example, the following code uses find_end() to search for two different sequences of numbers. The the first chunk of code, the last occurence of "1 2 3" is found. In the second chunk of code, the sequence that is being searched for is not found:

 int nums[] = { 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4 };
 int* result;
 int start = 0;
 int end = 11;          

 int target1[] = { 1, 2, 3 };
 result = find_end( nums + start, nums + end, target1 + 0, target1 + 2 );
 if( *result == nums[end] ) {
   cout << "Did not find any subsequence matching { 1, 2, 3 }" << endl;
 } else {
   cout << "The last matching subsequence is at: " << *result << endl;
 }              

 int target2[] = { 3, 2, 3 };
 result = find_end( nums + start, nums + end, target2 + 0, target2 + 2 );
 if( *result == nums[end] ) {
   cout << "Did not find any subsequence matching { 3, 2, 3 }" << endl;
 } else {
   cout << "The last matching subsequence is at: " << *result << endl;
 }              
Related topics: