📄 equ_0708.htm
字号:
<HTML><TITLE>equal</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>equal</H2>
<HR><PRE> Algorithm</PRE><HR>
<A NAME="Summary"><H3>Summary</H3></A>
<P>Compares two ranges for equality.</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>
</UL>
<A NAME="Synopsis"><H3>Synopsis</H3></A>
<PRE>#include <algorithm></PRE>
<PRE>
template <class InputIterator1, class InputIterator2>
bool equal(InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2);
template <class InputIterator1, class InputIterator2,
class BinaryPredicate>
bool equal(InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, BinaryPredicate binary_pred);
</PRE>
<A NAME="Description"><H3>Description</H3></A>
<P>The <B><I>equal</B></I> algorithm does a pairwise comparison of all of the elements in one range with all of the elements in another range to see if they match. The first version of <B><I>equal</B></I> uses the equal operator (==) as the comparison function, and the second version allows you to specify a binary predicate as the comparison function. The first version returns <SAMP>true</SAMP> if all of the corresponding elements are equal to each other. The second version of <B><I>equal</B></I> returns <SAMP>true</SAMP> if for each pair of elements in the two ranges, the result of applying the binary predicate is <SAMP>true</SAMP>. In other words, <B><I>equal</B></I> returns <SAMP>true</SAMP> if both of the following are true:</P>
<OL><LI><P>There are at least as many elements in the second range as in the first;</P>
</LI>
<LI><P>For every iterator <SAMP>i</SAMP> in the range <SAMP>[first1, last1)</SAMP> the following corresponding conditions hold: </P>
<PRE> *i == *(first2 + (i - first1))</PRE>
</LI>
<P> or</P>
<PRE> binary_pred(*i, *(first2 + (i - first1))) == true</PRE>
</OL>
<P>Otherwise, <B><I>equal</B></I> returns <SAMP>false</SAMP>.</P>
<P>This algorithm assumes that there are at least as many elements available after <SAMP>first2</SAMP> as there are in the range <SAMP>[first1, last1).</SAMP></P>
<A NAME="Complexity"><H3>Complexity</H3></A>
<P><B><I>equal</B></I> performs at most <SAMP>last1-first1</SAMP> comparisons or applications of the predicate.</P>
<A NAME="Example"><H3>Example</H3></A>
<PRE>//
// equal.cpp
//
#include <algorithm>
#include <vector>
#include <iostream.h>
int main()
{
int d1[4] = {1,2,3,4};
int d2[4] = {1,2,4,3};
//
// Set up two vectors
//
vector<int> v1(d1+0, d1 + 4), v2(d2+0, d2 + 4);
// Check for equality
bool b1 = <B>equal</B>(v1.begin(),v1.end(),v2.begin());
bool b2 = <B>equal</B>(v1.begin(),v1.end(),
v2.begin(),equal_to<int>());
// Both b1 and b2 are false
cout << (b1 ? "TRUE" : "FALSE") << " "
<< (b2 ? "TRUE" : "FALSE") << endl;
return 0;
}
Output :
FALSE FALSE
</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>
<HR>
<A HREF="div_3309.htm"><IMG SRC="images/prev.gif"></A> <A HREF="ref.htm#contents"><IMG SRC="images/toc.gif"></A> <A HREF="equ_3232.htm"><IMG SRC="images/next.gif"></A></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -