📄 fin_7707.htm
字号:
<HTML><TITLE>find_end</TITLE><BODY>
<A HREF="ref.htm"><IMG SRC="images/banner.gif"></A>
<P><STRONG>Click on the banner to return to the Class Reference home page.</STRONG></P>
<P>©Copyright 1996 Rogue Wave Software</P>
<H2>find_end</H2>
<HR><PRE><B><I> </I></B>Algorithm</B></I>
</PRE><HR>
<A NAME="Summary"><H3>Summary</H3></A>
<P>Finds a subsequence of equal values in a sequence.</P>
<H3>Contents</H3>
<UL>
<A HREF="#Synopsis"><LI>Synopsis</LI></A>
<A HREF="#Description"><LI>Description</LI></A>
<A HREF="#Complexity"><LI>Complexity</LI></A>
<A HREF="#Example"><LI>Example</LI></A>
<A HREF="#Warnings"><LI>Warnings</LI></A>
<A HREF="#See Also"><LI>See Also</LI></A>
</UL>
<A NAME="Synopsis"><H3>Synopsis</H3></A>
<PRE>#include <algorithm></PRE>
<PRE>
template <class ForwardIterator1, class ForwardIterator2></PRE>
<PRE>ForwardIterator1 <B>find_end</B>(ForwardIterator1 first1,
ForwardIterator1 last1,
ForwardIterator2 first2,
ForwardIterator2 last2);
template <class Forward Iterator1, class ForwardIterator2,
class BinaryPredicate>
ForwardIterator1 <B>find_end</B>(ForwardIterator1 first1,
ForwardIterator1 last1,
ForwardIterator2 first2,
ForwardIterator2 last2,
BinaryPredicate pred);
</PRE>
<A NAME="Description"><H3>Description</H3></A>
<P>The<B><I> find_end </B></I>algorithm finds the last occurrence of a subsequence, indicated by <SAMP>[first2, last2)</SAMP>, in a sequence, <SAMP>[first1,last1)</SAMP>. The algorithm returns an iterator pointing to the first element of the found subsequence, or <SAMP>last1</SAMP> if no match is found.</P>
<P>More precisely, the<B><I> find_end </B></I>algorithm returns the last iterator <SAMP>i</SAMP> in the range <SAMP>[first1, last1 - (last2-first2))</SAMP> such that for any non-negative integer <SAMP>n < (last2-first2)</SAMP>, the following corresponding conditions hold: </P>
<PRE>*(i+n) == *(first2+n), </PRE>
<PRE>pred(*(i+n),*(first2+n)) == true.
</PRE><P>Or returns <SAMP>last1</SAMP> if no such iterator is found.</P>
<P>Two versions of the algorithm exist. The first uses the equality operator as the default binary predicate, and the second allows you to specify a binary predicate.</P>
<A NAME="Complexity"><H3>Complexity</H3></A>
<P>At most <SAMP>(last2-first2)*(last1-first1-(last2-first2)+1)</SAMP> applications of the corresponding predicate are done.</P>
<A NAME="Example"><H3>Example</H3></A>
<PRE>//
// find_end.cpp
//
#include<vector>
#include<iterator>
#include<algorithm>
#include<iostream.h>
int main()
{
typedef vector<int>::iterator iterator;
int d1[10] = {0,1,6,5,3,2,2,6,5,7};
int d2[4] = {6,5,0,0}</PRE>
<PRE> //
// Set up two vectors.
//
vector<int> v1(d1+0, d1+10), v2(d2+0, d2+2);
//
// Try both find_first_of variants.
//
iterator it1 = find_first_of (v1.begin(), v1.end(), v2.begin(),
v2.end());</PRE>
<PRE> iterator it2 = find_first_of (v1.begin(), v1.end(), v2.begin(),
v2.end(), equal_to<int>());
//
// Try both find_end variants.
//
iterator it3 = <B>find_end</B> (v1.begin(), v1.end(), v2.begin(),
v2.end());</PRE>
<PRE> iterator it4 = <B>find_end</B> (v1.begin(), v1.end(), v2.begin(),
v2.end(), equal_to<int>());
//
// Output results of find_first_of.
// Iterator now points to the first element that matches one of
// a set of values
//
cout << "For the vectors: ";
copy (v1.begin(), v1.end(), ostream_iterator<int>(cout," "));
cout << " and ";
copy (v2.begin(), v2.end(), ostream_iterator<int>(cout," "));
cout<< endl ,, endl
<< "both versions of find_first_of point to: "
<< *it1 << endl << "with first_of address = " << it1
<< endl ;
//
//Output results of find_end.
// Iterator now points to the first element of the last find
//subsequence.
//
cout << endl << endl
<< "both versions of find_end point to: "
<< *it3 << endl << "with find_end address = " << it3
<< endl ;
return 0;
}
Output :
For the vectors: 0 1 6 5 3 2 2 6 5 7 and 6 5
both versions of find_first_of point to: 6
with first_of address = 0x100005c0
both versions of find_end point to: 6
with find_end address = 0x100005d4</PRE>
<A NAME="Warnings"><H3>Warnings</H3></A>
<P>If your compiler does not support default template parameters then you need to always supply the <SAMP>Allocator</SAMP> template argument. For instance you'll have to write:</P>
<PRE>vector<int, allocator></PRE>
<PRE></PRE><P>instead of:</P>
<PRE>vector<int></PRE>
<A NAME="See Also"><H3>See Also</H3></A>
<P><A HREF="Alg_5157.htm"><B><I>Algorithms</B></I></A>, <A HREF="fin_7988.htm"><B><I>find</B></I></A>, <A HREF="fin_2105.htm"><B><I>find_if</B></I></A>, <A HREF="adj_8817.htm"><B><I>adjacent_find</B></I></A></P>
<HR>
<A HREF="fin_7988.htm"><IMG SRC="images/prev.gif"></A> <A HREF="ref.htm#contents"><IMG SRC="images/toc.gif"></A> <A HREF="fin_8583.htm"><IMG SRC="images/next.gif"></A></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -