stdcxx-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From se...@apache.org
Subject svn commit: r699390 - /stdcxx/branches/4.2.x/doc/stdlibref/bind1st.html
Date Fri, 26 Sep 2008 16:11:08 GMT
Author: sebor
Date: Fri Sep 26 09:11:07 2008
New Revision: 699390

URL: http://svn.apache.org/viewvc?rev=699390&view=rev
Log:
2008-09-26  Martin Sebor  <sebor@roguewave.com>

	* doc/stdlibref/bind1st.html (Example): Updated to sync with rev 699379.

Modified:
    stdcxx/branches/4.2.x/doc/stdlibref/bind1st.html

Modified: stdcxx/branches/4.2.x/doc/stdlibref/bind1st.html
URL: http://svn.apache.org/viewvc/stdcxx/branches/4.2.x/doc/stdlibref/bind1st.html?rev=699390&r1=699389&r2=699390&view=diff
==============================================================================
--- stdcxx/branches/4.2.x/doc/stdlibref/bind1st.html (original)
+++ stdcxx/branches/4.2.x/doc/stdlibref/bind1st.html Fri Sep 26 09:11:07 2008
@@ -125,52 +125,65 @@
 
 #include &lt;algorithm&gt;   // for find_if
 #include &lt;functional&gt;  // for equal_to, bind1st, bind2nd
-#include &lt;iostream&gt;    // for cout, endl
+#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, std::allocator&lt;int&gt; &gt; vector;
-    typedef std::equal_to&lt;vector::value_type&gt;      equal_to;
+    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 };
 
-    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&lt;equal_to&gt; 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&lt;EqualTo&gt; 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 &lt;&lt; *it1 &lt;&lt; " " &lt;&lt; *it2 &lt;&lt;
" " 
-              &lt;&lt; *it3 &lt;&lt; std::endl;
+    std::ostream_iterator&lt;Vector::value_type&gt; out (std::cout, " ");
 
-    return 0;
-}
+    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:
-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>



Mime
View raw message