You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@stdcxx.apache.org by se...@apache.org on 2005/12/09 17:45:02 UTC
svn commit: r355532 - /incubator/stdcxx/trunk/examples/manual/list.cpp
Author: sebor
Date: Fri Dec 9 08:44:58 2005
New Revision: 355532
URL: http://svn.apache.org/viewcvs?rev=355532&view=rev
Log:
2005-12-09 Martin Sebor <se...@roguewave.com>
* list.cpp (operator<<): Added comments.
(main): Replaced endl with '\n' for efficiency.
Modified:
incubator/stdcxx/trunk/examples/manual/list.cpp
Modified: incubator/stdcxx/trunk/examples/manual/list.cpp
URL: http://svn.apache.org/viewcvs/incubator/stdcxx/trunk/examples/manual/list.cpp?rev=355532&r1=355531&r2=355532&view=diff
==============================================================================
--- incubator/stdcxx/trunk/examples/manual/list.cpp (original)
+++ incubator/stdcxx/trunk/examples/manual/list.cpp Fri Dec 9 08:44:58 2005
@@ -2,7 +2,7 @@
*
* list.cpp - Example program of list class.
*
- * $Id: //stdlib/dev/examples/stdlib/manual/list.cpp#13 $
+ * $Id$
*
***************************************************************************
*
@@ -22,7 +22,7 @@
#include <algorithm> // for find
#include <list> // for list
#include <string> // for string
-#include <iostream> // for cout, endl
+#include <iostream> // for cout
#include <examples.h>
@@ -33,18 +33,28 @@
// stream out a list of strings
std::ostream& operator<< (std::ostream &out, const StringList &l)
{
+ // create a sentry object to guard the stream
const std::ostream::sentry guard (out);
- for (StringList::const_iterator i = l.begin (); i != l.end (); ++i) {
+ if (guard) {
- const std::streamsize n =
- out.rdbuf ()->sputn ((*i).data (), (*i).size ());
+ // the guard succeeded in preparing the stream for output
- if ( std::streamsize ((*i).size ()) != n
- || std::ostream::traits_type::to_int_type (' ')
- != out.rdbuf ()->sputc (' ')) {
- out.setstate (std::ios::badbit);
- break;
+ for (StringList::const_iterator i = l.begin (); i != l.end (); ++i) {
+
+ // insert the string into the stream object's buffer
+ const std::streamsize n =
+ out.rdbuf ()->sputn ((*i).data (), (*i).size ());
+
+ // insert the space character into the buffer
+ if ( std::streamsize ((*i).size ()) != n
+ || std::ostream::traits_type::to_int_type (' ')
+ != out.rdbuf ()->sputc (' ')) {
+
+ // set badbit if either operation failed
+ out.setstate (std::ios::badbit);
+ break;
+ }
}
}
@@ -57,17 +67,17 @@
// Create a list of critters.
StringList critters;
- // Insert several critters.
+ // Insert a few critters.
critters.insert (critters.begin (), "antelope");
critters.insert (critters.begin (), "bear");
critters.insert (critters.begin (), "cat");
// Print out the list.
- std::cout << critters << std::endl;
+ std::cout << critters << '\n';
// Change cat to cougar.
*std::find (critters.begin (),critters.end (), "cat") = "cougar";
- std::cout << critters << std::endl;
+ std::cout << critters << '\n';
// Put a zebra at the beginning, an ocelot ahead of antelope,
// and a rat at the end.
@@ -75,20 +85,20 @@
critters.insert (std::find (critters.begin (), critters.end (),
"antelope"), "ocelot");
critters.push_back ("rat");
- std::cout << critters << std::endl;
+ std::cout << critters << '\n';
// Sort the list (Use list's sort function since the
// generic algorithm requires a random access iterator
// and list only provides bidirectional)
critters.sort ();
- std::cout << critters << std::endl;
+ std::cout << critters << '\n';
// Now let's erase half of the critters.
StringList::size_type half = critters.size () / 2;
for (StringList::size_type i = 0; i != half; ++i)
critters.erase (critters.begin ());
- std::cout << critters << std::endl;
+ std::cout << critters << '\n';
return 0;
}