Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ Moderated > Re: How to buil...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 20 of 30 Topic 9533 of 9830
Post > Topic >>

Re: How to build a string on the fly, using std::ostringstream? [Reposting]

by Vidar Hasfjord <vattilah-groups@[EMAIL PROTECTED] > Apr 29, 2008 at 10:43 AM

On Apr 28, 11:06 pm, vova...@[EMAIL PROTECTED]
 wrote:
> This can be done by changing a member
>    std::auto_ptr <std::ostringstream> s;
> with
>    std::stringstream s;
> and defining the copy constructor with
>    s << sb.s.rdbuf();
> in its body. Not sure if this approach is any better.

It does remove the need for dynamic allocation of the stream, but it
requires two instances of the stream and copying of the content
between them. Creating instances and copying will involve dynamic
allocation. Hence I doubt it is any more efficient than dynamic
allocation of the stream.

The underlying problem is that ostringstream is not swappable. While I
saw a glimmer of hope in using rdbuf to swap the stringbuffers,
unfortunately a derived stream may use its own buffer (not the one
returned by ios::rdbuf), and unfortunately ostringstream does just
that. It even hides the mutating version of rdbuf, so you cannot
replace the buffer.

A custom stream overcomes this problem:

   // Swappable string buffer

   class stringbuf
     : public std::stringbuf
   {
   public:
    explicit stringbuf ()
       : std::stringbuf (std::ios_base::out)
     {}

     void swap (stringbuf& other) throw ()
     {
       char* gbegin = other.eback ();
       char* gnext = other.gptr ();
       char* gend = other.egptr ();
       other.setg (eback (), gptr (), egptr ());
       setg (gbegin, gnext, gend);

       char* pbegin = other.pbase ();
       char* pnext = other.pptr ();
       char* pend = other.epptr ();
       other.setp (pbase (), pptr (), epptr ());
       setp (pbegin, pnext, pend);
     }
   };

   // String stream base

   typedef std::basic_ostream <char, std::char_traits <char> >
     ostringstream_base;

   // Swappable string stream

   class ostringstream
     : public ostringstream_base
   {
   public:
     ostringstream ()
       : ostringstream_base (&buf)
     {}

     void swap (ostringstream& other) throw ()
     {buf.swap (other.buf);}

     stringbuf* rdbuf()
     {return &buf;}

     std::string str () const
     {return buf.str();}

     void str (const std::string& s)
     {buf.str (s);}

   private:
     stringbuf buf;
   };

Regards,
Vidar Hasfjord

-- 
      [ See http://www.gotw.ca/resources/clcm.htm
for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
 




 30 Posts in Topic:
How to build a string on the fly, using std::ostringstream?
"Niels Dekker - no r  2008-04-23 13:14:37 
Re: How to build a string on the fly, using std::ostringstream?
Martin York <Martin.Yo  2008-04-24 01:05:54 
Re: How to build a string on the fly, using std::ostringstream?
Peter Jones <dev-null@  2008-04-24 01:10:33 
Re: How to build a string on the fly, using std::ostringstream?
Carl Barron <cbarron41  2008-04-24 01:43:21 
Re: How to build a string on the fly, using std::ostringstream?
Vidar Hasfjord <vattil  2008-04-24 15:12:53 
Re: How to build a string on the fly, using std::ostringstream?
Brendan <catphive@[EMA  2008-04-24 15:43:20 
Re: How to build a string on the fly, using std::ostringstream?
Brendan <catphive@[EMA  2008-04-25 10:22:59 
Re: How to build a string on the fly, using std::ostringstream?
Niels Dekker - no return   2008-04-26 08:46:24 
Re: How to build a string on the fly, using std::ostringstream?
Carl Barron <cbarron41  2008-04-27 07:13:20 
Re: How to build a string on the fly, using std::ostringstream?
Brendan Miller <catphi  2008-04-27 07:29:25 
Re: How to build a string on the fly, using std::ostringstream?
Vidar Hasfjord <vattil  2008-04-27 07:29:42 
Re: How to build a string on the fly, using std::ostringstream?
Vidar Hasfjord <vattil  2008-04-28 01:56:19 
Re: How to build a string on the fly, using std::ostringstream?
Vidar Hasfjord <vattil  2008-04-28 02:13:22 
Re: How to build a string on the fly, using std::ostringstream?
vova777@[EMAIL PROTECTED]  2008-04-28 16:06:27 
Re: How to build a string on the fly, using std::ostringstream?
"Niels Dekker - no r  2008-04-28 16:01:35 
Re: How to build a string on the fly, using std::ostringstream?
guinness.tony@[EMAIL PROT  2008-04-28 16:01:50 
Re: How to build a string on the fly, using std::ostringstream?
"Hendrik Schober&quo  2008-04-28 20:35:47 
Re: How to build a string on the fly, using std::ostringstream?
Vidar Hasfjord <vattil  2008-04-29 10:38:48 
Re: How to build a string on the fly, using std::ostringstream?
Brendan Miller <catphi  2008-04-29 10:43:23 
Re: How to build a string on the fly, using std::ostringstream?
Vidar Hasfjord <vattil  2008-04-29 10:43:23 
Re: How to build a string on the fly, using std::ostringstream?
Vidar Hasfjord <vattil  2008-04-29 10:43:22 
Re: How to build a string on the fly, using std::ostringstream?
vova777@[EMAIL PROTECTED]  2008-04-29 12:13:56 
Re: How to build a string on the fly, using std::ostringstream?
Vidar Hasfjord <vattil  2008-04-29 17:41:45 
Re: How to build a string on the fly, using std::ostringstream?
Niels Dekker - no return   2008-04-30 10:26:51 
Re: How to build a string on the fly, using std::ostringstream?
vova777@[EMAIL PROTECTED]  2008-04-30 10:39:18 
Re: How to build a string on the fly, using std::ostringstream?
Vidar Hasfjord <vattil  2008-04-30 17:04:43 
Re: How to build a string on the fly, using std::ostringstream?
guinness.tony@[EMAIL PROT  2008-05-01 13:12:58 
Re: How to build a string on the fly, using std::ostringstream?
"Niels Dekker - no r  2008-05-02 12:45:30 
Re: How to build a string on the fly, using std::ostringstream?
guinness.tony@[EMAIL PROT  2008-05-07 11:43:29 
Re: How to build a string on the fly, using std::ostringstream?
"Niels Dekker - no r  2008-05-08 21:29:05 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Fri Jul 25 15:43:43 CDT 2008.