find_end |
Locate the last item in a sequence.
Searches the sequence [ first, last1 ) for the subsequence [ first2, last2 ). If a single match is found, an iterator pointing to the first element in the subrange is returned. If multiple matches are found, the returned iterator points to the first element in the last matching sequence. The iterator last1 is returned if no matches are found.
#include <algorithm>
template
<
class ForwardIterator1,
class ForwardIterator2
>
InputIterator1 find_end
(
ForwardIterator1 first1,
ForwardIterator1 last1,
ForwardIterator2 first2,
ForwardIterator2 last2
);
template
<
class ForwardIterator1,
class ForwardIterator2,
class BinaryPredicate
>
InputIterator1 find_end
(
ForwardIterator1 first1,
ForwardIterator1 last1,
ForwardIterator2 first2,
ForwardIterator2 last2
);
The find_end
algorithm performs at most ( last2 - first2
) * ( last1 - first1 - (
last2 - first2 ) + 1) applications of the
corresponding predicate.
#include <iostream>
#include <algorithm>
char seq1[ 8 ] = "abcqxyz";
const char seq2[ 6 ] = "abqyz";
void
main()
{
char* end = find_end( seq1, seq1 + 8, seq2, seq2 + 6 );
if ( end == seq1 + 8 )
cout << "No subsequence found.\n";
else
cout
<< "The sequences are the same for the first "
<< ( end - seq1 + 1 )
<< " elements.\n";
}
The sequences are the same for the first 2 elements.
#include <iostream>
#include <ctype.h>
#include <algorithm>
char sequence1[ 8 ] = "abcQxyz";
const char sequence2[ 6 ] = "abQyz";
bool
both_lower( const char& a, const char& b )
{
return islower( a ) && islower( b );
}
void
main()
{
char* end = find_end
(
sequence1,
sequence1 + 8,
sequence2,
sequence2 + 6,
both_lower
);
if ( end == sequence1 + 8 )
cout << "No lowercase characters found.\n";
else
cout
<< "The sequences are both in lowercase for the first "
<< ( end - sequence1 + 1 )
<< " elements.\n";
}
The sequences are both in lowercase for the first 2 elements.
Copyright©1994-2026 Recursion Software LLC
All Rights Reserved - For use by licensed users only.