blob: 5761973ea824ec136914f1b08b486a3f12166751 [file] [log] [blame]
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed
with this work for additional information regarding copyright
ownership. The ASF licenses this file to you under the Apache
License, Version 2.0 (the License); you may not use this file
except in compliance with the License. You may obtain a copy of
the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied. See the License for the specific language governing
permissions and limitations under the License.
Copyright 1999-2007 Rogue Wave Software, Inc.
-->
<HTML>
<HEAD>
<TITLE>bind1st(), bind2nd(), binder1st, binder2nd</TITLE>
<LINK REL=StyleSheet HREF="../rw.css" TYPE="text/css" TITLE="Apache stdcxx Stylesheet"></HEAD>
<BODY BGCOLOR=#FFFFFF>
<A HREF="binary-search.html"><IMG SRC="images/bprev.gif" WIDTH=20 HEIGHT=21 ALT="Previous file" BORDER=O></A><A HREF="noframes.html"><IMG SRC="images/btop.gif" WIDTH=56 HEIGHT=21 ALT="Top of Document" BORDER=O></A><A HREF="booktoc.html"><IMG SRC="images/btoc.gif" WIDTH=56 HEIGHT=21 ALT="Contents" BORDER=O></A><A HREF="tindex.html"><IMG SRC="images/bindex.gif" WIDTH=56 HEIGHT=21 ALT="Index page" BORDER=O></A><A HREF="bitmasktypes.html"><IMG SRC="images/bnext.gif" WIDTH=25 HEIGHT=21 ALT="Next file" BORDER=O></A><DIV CLASS="DOCUMENTNAME"><B>Apache C++ Standard Library Reference Guide</B></DIV>
<H2>bind1st(), bind2nd(), binder1st, binder2nd</H2>
<P><B>Library:</B>&nbsp;&nbsp;<A HREF="2-4.html">General utilities</A></P>
<PRE><HR><B><I>Function</I></B><HR></PRE>
<UL>
<LI><A HREF="#sec1">Local Index</A></LI>
<LI><A HREF="#sec2">Summary</A></LI>
<LI><A HREF="#sec3">Synopsis</A></LI>
<LI><A HREF="#sec4">Description</A></LI>
<LI><A HREF="#sec5">Interface</A></LI>
<LI><A HREF="#sec6">Example</A></LI>
<LI><A HREF="#sec7">See Also</A></LI>
<LI><A HREF="#sec8">Standards Conformance</A></LI>
</UL>
<A NAME="sec1"><H3>Local Index</H3></A>
No Entries
<A NAME="sec2"><H3>Summary</H3></A>
<P>Templatized utilities to bind values to function objects</P>
<A NAME="sec3"><H3>Synopsis</H3></A>
<PRE>#include &lt;functional&gt;
namespace std {
template &lt;class Operation&gt;
class binder1st;
template &lt;class Operation, class T&gt;
binder1st&lt;Operation&gt; bind1st(const Operation&amp;, const T&amp;);
template &lt;class Operation&gt;
class binder2nd ;
template &lt;class Operation, class T&gt;
binder2nd&lt;Operation&gt; bind2nd(const Operation&amp;, const T&amp;);
}
</PRE>
<A NAME="sec4"><H3>Description</H3></A>
<P>Because so many functions included in the C++ Standard Library take other functions as arguments, the library includes classes that let you build new function objects out of old ones. Both <SAMP><A HREF="bind1st.html">bind1st()</A></SAMP> and <SAMP><A HREF="bind1st.html">bind2nd()</A></SAMP> are functions that take as arguments a binary function object <SAMP>f</SAMP> and a value <SAMP>x,</SAMP> and return, respectively, classes <B><I><A HREF="bind1st.html">binder1st</A></I></B> and <B><I><A HREF="bind1st.html">binder2nd</A></I></B>. The underlying function object must be a subclass of <B><I><A HREF="binary-function.html">binary_function</A></I></B>.</P>
<P>Class <B><I><A HREF="bind1st.html">binder1st</A></I></B> binds the value to the first argument of the binary function, and <B><I><A HREF="bind1st.html">binder2nd</A></I></B> does the same thing for the second argument of the function. The resulting classes can be used in place of a unary predicate in other function calls.</P>
<P>For example, you could use the <SAMP><A HREF="count.html">count_if()</A></SAMP> algorithm to count all elements in a <B><I><A HREF="vector.html">vector</A></I></B> that are less than or equal to 7, using the following:</P>
<UL><PRE>
vector&lt;int&gt; v;<br>int littleNums;<br>
count_if(v.begin, v.end, bind1st(greater&lt;int&gt;(),7),
littleNums)
</PRE></UL>
<P>This function adds one to <SAMP>littleNums</SAMP> each time the element is greater than&nbsp;7.</P>
<A NAME="sec5"><H3>Interface</H3></A>
<UL><PRE>namespace std {
// Class binder1st
template &lt;class Operation&gt;
class binder1st
: public unary_function&lt;typename
Operation::second_argument_type,
typename Operation::result_type&gt;
{
public:
binder1st(const Operation&amp;,
const typename Operation::first_argument_type&amp;);
typename Operation::result_type
operator() (const typename
Operation::second_argument_type&amp;) const;
};
// Class binder2nd
template &lt;class Operation&gt;
class binder2nd
: public unary_function&lt;typename
Operation::first_argument_type,
typename Operation::result_type&gt;
{
public:
binder2nd(const Operation&amp;,
const typename Operation::second_argument_type&amp;);
typename Operation::result_type
operator()(const typename
Operation::first_argument_type&amp;) const;
};
// Creator bind1st
template &lt;class Operation, class T&gt;
binder1st&lt;Operation&gt; bind1st(const Operation&amp;, const T&amp;);
// Creator bind2nd
template&lt;class Operation, class T&gt;
binder2nd &lt;Operation&gt; bind2nd(const Operation&amp;, const T&amp;);
}
</PRE></UL>
<A NAME="sec6"><H3>Example</H3></A>
<UL><PRE>//
// binders.cpp
//
#include &lt;algorithm&gt; // for find_if
#include &lt;functional&gt; // for equal_to, bind1st, bind2nd
#include &lt;iostream&gt; // for cout
#include &lt;iterator&gt; // for ostream_iterator
#include &lt;vector&gt; // for vector
int main ()
{
typedef std::vector&lt;int&gt; Vector;
typedef std::equal_to&lt;Vector::value_type&gt; EqualTo;
const Vector::value_type arr [] = { 1, 2, 3, 4, 5 };
// Initialize a vector with the array elements.
const Vector v1 (arr, arr + sizeof arr / sizeof *arr);
// Value to look for.
const Vector::value_type x (3);
// Create an 'equal to 3' unary predicate by binding the value
// 3 to the EqualTo binary predicate.
const std::binder1st&lt;EqualTo&gt; equal_to_3 =
std::bind1st (EqualTo (), x);
// Now use this new predicate in a call to find_if.
const Vector::const_iterator it1 =
std::find_if (v1.begin (), v1.end (), equal_to_3);
// Even better, construct the new predicate on the fly.
const Vector::const_iterator it2 =
std::find_if (v1.begin (), v1.end (), std::bind1st (EqualTo (), x));
// And now the same thing using bind2nd.
// Same result since EqualTo is commutative.
const Vector::const_iterator it3 =
std::find_if (v1.begin (), v1.end (), std::bind2nd (EqualTo (), x));
// Use the same predicate to count the number of elements
// equal to 3.
const Vector::size_type n =
std::count_if (v1.begin (), v1.end (), std::bind2nd (EqualTo (), x));
// Output results.
std::ostream_iterator&lt;Vector::value_type&gt; out (std::cout, " ");
std::cout &lt;&lt; "The vector { ";
std::copy (v1.begin (), v1.end (), out);
std::cout &lt;&lt; "} contains " &lt;&lt; n &lt;&lt; " element equal to "
&lt;&lt; x &lt;&lt; " at offset " &lt;&lt;
it1 - v1.begin () &lt;&lt; ".\n";
// Exit with status of 0 on success, 1 on failure.
const bool success = 1 == n && it1 == it2 && it1 == it2 && *it1 == x;
return success ? 0 : 1;
}
Program Output:
The vector { 1 2 3 4 5 } contains 1 element equal to 3 at offset 2.
</PRE></UL>
<UL><PRE></PRE></UL>
<A NAME="sec7"><H3>See Also</H3></A>
<P><A HREF="functionobjects.html">Function Objects</A></P>
<A NAME="sec8"><H3>Standards Conformance</H3></A>
<P><I>ISO/IEC 14882:1998 -- International Standard for Information Systems -- Programming Language C++, Section 20.3.6</I></P>
<BR>
<HR>
<A HREF="binary-search.html"><IMG SRC="images/bprev.gif" WIDTH=20 HEIGHT=21 ALT="Previous file" BORDER=O></A><A HREF="noframes.html"><IMG SRC="images/btop.gif" WIDTH=56 HEIGHT=21 ALT="Top of Document" BORDER=O></A><A HREF="booktoc.html"><IMG SRC="images/btoc.gif" WIDTH=56 HEIGHT=21 ALT="Contents" BORDER=O></A><A HREF="tindex.html"><IMG SRC="images/bindex.gif" WIDTH=56 HEIGHT=21 ALT="Index page" BORDER=O></A><A HREF="bitmasktypes.html"><IMG SRC="images/bnext.gif" WIDTH=20 HEIGHT=21 ALT="Next file" BORDER=O></A>
<!-- Google Analytics tracking code -->
<script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-1775151-1";
urchinTracker();
</script>
<!-- end of Google Analytics tracking code -->
</BODY>
</HTML>