📄 lex_3806.htm
字号:
<HTML><TITLE>lexicographical_compare</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>lexicographical_compare</H2>
<HR><PRE> Algorithm</PRE><HR>
<A NAME="Summary"><H3>Summary</H3></A>
<P>Compares two ranges lexicographically.</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="#Warning"><LI>Warning</LI></A>
</UL>
<A NAME="Synopsis"><H3>Synopsis</H3></A>
<PRE>#include <algorithm></PRE>
<PRE>
template <class InputIterator1, class InputIterator2>
bool
<B>lexicographical_compare</B>(InputIterator1 first,
InputIterator2 last1,
InputIterator2 first2,
InputIterator last2);
template <class InputIterator1, class InputIterator2, class Compare>
bool
<B>lexicographical_compare</B>(InputIterator1 first,
InputIterator2 last1,
InputIterator2 first2,
InputIterator last2, Compare comp);</PRE>
<A NAME="Description"><H3>Description</H3></A>
<P>The <B><I>lexicographical_compare</B></I> functions compare each element in the range <SAMP>[first1, last1)</SAMP> to the corresponding element in the range <SAMP>[first2, last2)</SAMP> using iterators <SAMP>i</SAMP> and <SAMP>j</SAMP>. </P>
<P>The first version of the algorithm uses <SAMP>operator<</SAMP> as the default comparison operator. It immediately returns <SAMP>true</SAMP> if it encounters any pair in which <SAMP>*i</SAMP> is less than <SAMP>*j</SAMP>, and immediately returns <SAMP>false</SAMP> if <SAMP>*j</SAMP> is less than <SAMP>*i</SAMP>. If the algorithm reaches the end of the first sequence before reaching the end of the second sequence, it also returns <SAMP>true</SAMP>. </P>
<P>The second version of the function takes an argument <SAMP>comp</SAMP> that defines a comparison function that is used in place of the default <SAMP>operator<.</SAMP></P>
<P>The <B><I>lexicographical_compare</B></I> functions can be used with all the datatypes provided by the standard library.</P>
<A NAME="Complexity"><H3>Complexity</H3></A>
<P><B><I>lexicographical_compare</B></I> performs at most <SAMP>min((last1 - first1), (last2 - first2))</SAMP> applications of the comparison function.</P>
<A NAME="Example"><H3>Example</H3></A>
<PRE>//
// lex_comp.cpp
//
#include <algorithm>
#include <vector>
#include <iostream.h>
int main(void)
{
int d1[5] = {1,3,5,32,64};
int d2[5] = {1,3,2,43,56};
// set up vector
vector<int> v1(d1,d1 + 5), v2(d2,d2 + 5);
// Is v1 less than v2 (I think not)
bool b1 = <B>lexicographical_compare</B>(v1.begin(),
v1.end(), v2.begin(), v2.end());
// Is v2 less than v1 (yup, sure is)
bool b2 = <B>lexicographical_compare</B>(v2.begin(),
v2.end(), v1.begin(), v1.end(), less<int>());
cout << (b1 ? "TRUE" : "FALSE") << " "
<< (b2 ? "TRUE" : "FALSE") << endl;
return 0;
}
Output:
FALSE TRUE
</PRE>
<A NAME="Warning"><H3>Warning</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>
<P><SAMP>vector<int, allocator></SAMP></P>
<P>instead of :</P>
<P><SAMP>vector<int></SAMP></P>
<HR>
<A HREF="les_5595.htm"><IMG SRC="images/prev.gif"></A> <A HREF="ref.htm#contents"><IMG SRC="images/toc.gif"></A> <A HREF="lim_2532.htm"><IMG SRC="images/next.gif"></A></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -