📄 pre_1157.htm
字号:
<HTML><HEAD><TITLE>Predicates</TITLE></HEAD>
<BODY>
<A HREF="ug.htm"><IMG SRC="images/banner.gif"></A>
<P><STRONG>Click on the banner to return to the user guide home page.</STRONG></P>
<P>©Copyright 1996 Rogue Wave Software</P>
<H2>Predicates</H2>
<P>A <I>predicate</I> is simply a function that returns either a boolean (true/false) value or an integer value. Following the normal C convention, an integer value is assumed to be true if non-zero, and false otherwise. An example function might be the following, which takes as argument an integer and returns <SAMP>true</SAMP> if the number represents a leap year, and <SAMP>false</SAMP> otherwise:</P>
<PRE>bool isLeapYear (unsigned int year)
// return true if year is leap year
{
// millennia are leap years
if (0 == year % 1000) return true;
// every fourth century is
if (0 == year % 400) return true;
// every fourth year is
if (0 == year % 4) return true;
// otherwise not
return false;
}</PRE>
<P>A predicate is used as an argument, for example, in the generic algorithm named <SAMP>find_if()</SAMP>. This algorithm returns the first value that satisfies the predicate, returning the end-of-range value if no such element is found. Using this algorithm, the following locates the first leap year in a list of years:</P>
<PRE>list<int>::iterator firstLeap =
find_if(aList.begin(), aList.end(), isLeapYear);</PRE>
<HR>
<A HREF="fun_5137.htm"><IMG SRC="images/prev.gif"></A> <A HREF="booktoc.htm"><IMG SRC="images/toc.gif"></A> <A HREF="fun_0476.htm"><IMG SRC="images/next.gif"></A></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -