| <!-- |
| 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> <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 <functional> |
| |
| namespace std { |
| template <class Operation> |
| class binder1st; |
| |
| template <class Operation, class T> |
| binder1st<Operation> bind1st(const Operation&, const T&); |
| |
| template <class Operation> |
| class binder2nd ; |
| |
| template <class Operation, class T> |
| binder2nd<Operation> bind2nd(const Operation&, const T&); |
| } |
| </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<int> v;<br>int littleNums;<br> |
| count_if(v.begin, v.end, bind1st(greater<int>(),7), |
| littleNums) |
| </PRE></UL> |
| <P>This function adds one to <SAMP>littleNums</SAMP> each time the element is greater than 7.</P> |
| <A NAME="sec5"><H3>Interface</H3></A> |
| |
| <UL><PRE>namespace std { |
| |
| // Class binder1st |
| template <class Operation> |
| class binder1st |
| : public unary_function<typename |
| Operation::second_argument_type, |
| typename Operation::result_type> |
| { |
| public: |
| |
| binder1st(const Operation&, |
| const typename Operation::first_argument_type&); |
| |
| typename Operation::result_type |
| operator() (const typename |
| Operation::second_argument_type&) const; |
| }; |
| |
| // Class binder2nd |
| template <class Operation> |
| class binder2nd |
| : public unary_function<typename |
| Operation::first_argument_type, |
| typename Operation::result_type> |
| { |
| public: |
| |
| binder2nd(const Operation&, |
| const typename Operation::second_argument_type&); |
| typename Operation::result_type |
| operator()(const typename |
| Operation::first_argument_type&) const; |
| }; |
| |
| // Creator bind1st |
| template <class Operation, class T> |
| binder1st<Operation> bind1st(const Operation&, const T&); |
| |
| // Creator bind2nd |
| template<class Operation, class T> |
| binder2nd <Operation> bind2nd(const Operation&, const T&); |
| } |
| </PRE></UL> |
| <A NAME="sec6"><H3>Example</H3></A> |
| |
| <UL><PRE>// |
| // binders.cpp |
| // |
| |
| #include <algorithm> // for find_if |
| #include <functional> // for equal_to, bind1st, bind2nd |
| #include <iostream> // for cout, endl |
| #include <vector> // for vector |
| |
| |
| |
| int main () |
| { |
| typedef std::vector<int, std::allocator<int> > vector; |
| typedef std::equal_to<vector::value_type> equal_to; |
| |
| const vector::value_type arr [] = { 1, 2, 3, 4 }; |
| |
| // Set up a vector. |
| vector v1 (arr + 0, arr + sizeof arr / sizeof *arr); |
| |
| // Create an 'equal to 3' unary predicate by binding 3 to |
| // the equal_to binary predicate. |
| std::binder1st<equal_to> equal_to_3 = |
| bind1st (equal_to (), 3); |
| |
| // Now use this new predicate in a call to find_if. |
| vector::iterator it1 = std::find_if (v1.begin (), |
| v1.end (), |
| equal_to_3); |
| |
| // Even better, construct the new predicate on the fly. |
| vector::iterator it2 = |
| std::find_if (v1.begin (), v1.end (), |
| std::bind1st (equal_to (), 3)); |
| |
| // And now the same thing using bind2nd. |
| // Same result since equal_to is commutative. |
| vector::iterator it3 = |
| std::find_if (v1.begin (), v1.end (), |
| std::bind2nd (equal_to (), 3)); |
| |
| // Output results. |
| std::cout << *it1 << " " << *it2 << " " |
| << *it3 << std::endl; |
| |
| return 0; |
| } |
| |
| |
| Program Output: |
| 3 3 3 |
| </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> |