Author: sebor
Date: Fri Sep 26 09:22:41 2008
New Revision: 699393
URL: http://svn.apache.org/viewvc?rev=699393&view=rev
Log:
2008-09-26 Martin Sebor <[EMAIL PROTECTED]>
Merged revs 699379, 699389, 699390 from 4.2.x.
2008-09-26 Martin Sebor <[EMAIL PROTECTED]>
* examples/manual/binders.cpp (main): Constified locals and made
output more interesting. Exited with a non-zero status on failure.
* examples/manual/out/binders.out: Updated accordingly.
2008-09-26 Martin Sebor <[EMAIL PROTECTED]>
STDCXX-1017
* doc/stdlibref/bind1st.html (Description): Corrected confusing
text.
Updated code snippet to use the standard three-argument count_if()
algorithm instead of the obsolete four-argument extension.
2008-09-26 Martin Sebor <[EMAIL PROTECTED]>
* doc/stdlibref/bind1st.html (Example): Updated to sync with rev 699379.
Modified:
stdcxx/branches/4.3.x/doc/stdlibref/bind1st.html
stdcxx/branches/4.3.x/examples/manual/binders.cpp
stdcxx/branches/4.3.x/examples/manual/out/binders.out
Modified: stdcxx/branches/4.3.x/doc/stdlibref/bind1st.html
URL:
http://svn.apache.org/viewvc/stdcxx/branches/4.3.x/doc/stdlibref/bind1st.html?rev=699393&r1=699392&r2=699393&view=diff
==============================================================================
--- stdcxx/branches/4.3.x/doc/stdlibref/bind1st.html (original)
+++ stdcxx/branches/4.3.x/doc/stdlibref/bind1st.html Fri Sep 26 09:22:41 2008
@@ -64,14 +64,13 @@
<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>
+<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 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)
+std::vector<int> v (/* ... */);
+int littleNums = std::count_if (v.begin (), v.end (), std::bind1st
(std::less<int>(), 7));
</PRE></UL>
-<P>This function adds one to <SAMP>littleNums</SAMP> each time the element is
greater than 7.</P>
+<P>The function counts the number of elements in the range
[<SAMP>v.begin()</SAMP>, <SAMP>v.end()</SAMP>) as denoted by the first two
iterator arguments that satisfy the predicate specified by the third argument
and returns the result.</P>
<A NAME="sec5"><H3>Interface</H3></A>
<UL><PRE>namespace std {
@@ -126,52 +125,65 @@
#include <algorithm> // for find_if
#include <functional> // for equal_to, bind1st, bind2nd
-#include <iostream> // for cout, endl
+#include <iostream> // for cout
+#include <iterator> // for ostream_iterator
#include <vector> // for vector
-
int main ()
{
- typedef std::vector<int, std::allocator<int> > vector;
- typedef std::equal_to<vector::value_type> equal_to;
+ typedef std::vector<int> Vector;
+ typedef std::equal_to<Vector::value_type> EqualTo;
+
+ const Vector::value_type arr [] = { 1, 2, 3, 4, 5 };
- const vector::value_type arr [] = { 1, 2, 3, 4 };
+ // Initialize a vector with the array elements.
+ const Vector v1 (arr, arr + sizeof arr / sizeof *arr);
- // Set up a vector.
- vector v1 (arr + 0, arr + sizeof arr / sizeof *arr);
+ // Value to look for.
+ const Vector::value_type x (3);
- // 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);
+ // Create an 'equal to 3' unary predicate by binding the value
+ // 3 to the EqualTo binary predicate.
+ const std::binder1st<EqualTo> equal_to_3 =
+ std::bind1st (EqualTo (), x);
// Now use this new predicate in a call to find_if.
- vector::iterator it1 = std::find_if (v1.begin (),
- v1.end (),
- equal_to_3);
+ const Vector::const_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));
+ 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 equal_to is commutative.
- vector::iterator it3 =
- std::find_if (v1.begin (), v1.end (),
- std::bind2nd (equal_to (), 3));
+ // 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::cout << *it1 << " " << *it2 << " "
- << *it3 << std::endl;
+ std::ostream_iterator<Vector::value_type> out (std::cout, " ");
- return 0;
-}
+ std::cout << "The vector { ";
+ std::copy (v1.begin (), v1.end (), out);
+ std::cout << "} contains " << n << " element equal to "
+ << x << " at offset " <<
+ it1 - v1.begin () << ".\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:
-3 3 3
+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>
Modified: stdcxx/branches/4.3.x/examples/manual/binders.cpp
URL:
http://svn.apache.org/viewvc/stdcxx/branches/4.3.x/examples/manual/binders.cpp?rev=699393&r1=699392&r2=699393&view=diff
==============================================================================
--- stdcxx/branches/4.3.x/examples/manual/binders.cpp (original)
+++ stdcxx/branches/4.3.x/examples/manual/binders.cpp Fri Sep 26 09:22:41 2008
@@ -22,13 +22,14 @@
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*
- * Copyright 1994-2006 Rogue Wave Software.
+ * Copyright 1994-2006 Rogue Wave Software, Inc.
*
**************************************************************************/
#include <algorithm> // for find_if
#include <functional> // for equal_to, bind1st, bind2nd
-#include <iostream> // for cout, endl
+#include <iostream> // for cout
+#include <iterator> // for ostream_iterator
#include <vector> // for vector
#include <examples.h>
@@ -36,35 +37,50 @@
int main ()
{
- typedef std::vector<int, std::allocator<int> > Vector;
- typedef std::equal_to<Vector::value_type> equal_to;
+ typedef std::vector<int> Vector;
+ typedef std::equal_to<Vector::value_type> EqualTo;
- const Vector::value_type arr [] = { 1, 2, 3, 4 };
+ const Vector::value_type arr [] = { 1, 2, 3, 4, 5 };
- // Set up a vector.
- Vector v1 (arr + 0, arr + sizeof arr / sizeof *arr);
+ // Initialize a vector with the array elements.
+ const Vector v1 (arr, 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 = std::bind1st (equal_to (), 3);
+ // 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<EqualTo> equal_to_3 (std::bind1st (EqualTo (), x));
// Now use this new predicate in a call to find_if.
- Vector::iterator it1 = std::find_if (v1.begin (), v1.end (),
- equal_to_3);
+ const Vector::const_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));
+ 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 equal_to is commutative.
- Vector::iterator it3 =
- std::find_if (v1.begin (), v1.end (),
- std::bind2nd (equal_to (), 3));
+ // 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::cout << *it1 << " " << *it2 << " " << *it3 << std::endl;
+ std::ostream_iterator<Vector::value_type> out (std::cout, " ");
+
+ std::cout << "The vector { ";
+ std::copy (v1.begin (), v1.end (), out);
+
+ std::cout << "} contains " << n << " element equal to " << x
+ << " at offset " << it1 - v1.begin () << ".\n";
+
+ // Exit with status of 0 on success, 1 on failure.
+ const bool success = 1 == n && it1 == it2 && it1 == it2 && *it1 == x;
- return 0;
+ return success ? 0 : 1;
}
Modified: stdcxx/branches/4.3.x/examples/manual/out/binders.out
URL:
http://svn.apache.org/viewvc/stdcxx/branches/4.3.x/examples/manual/out/binders.out?rev=699393&r1=699392&r2=699393&view=diff
==============================================================================
--- stdcxx/branches/4.3.x/examples/manual/out/binders.out (original)
+++ stdcxx/branches/4.3.x/examples/manual/out/binders.out Fri Sep 26 09:22:41
2008
@@ -1 +1 @@
-3 3 3
+The vector { 1 2 3 4 5 } contains 1 element equal to 3 at offset 2.