Click here to Skip to main content
Licence 
First Posted 16 Dec 2002
Views 80,320
Bookmarked 17 times

Quick and convenient way to build STL strings.

By | 16 Dec 2002 | Article
A quick and convenient way to build STL strings.

Introduction

This is probably one of the shortest articles you'll ever see on Code Project but the one line (!) of code presented here I find so insanely handy, I thought that I'd share it with y'all. That, and I wanted to get my article count up a bit :-)

stringstream's are really useful for building strings. No more sprintf buffer overruns or tediously trying to match up parameters with their placeholders in the format string. As an example, this is how you would use one:

#include <sstream>

int theAnswer = 42 ;

// build the string 
std::ostringstream buf ; 
buf << "The answer is: " << theAnswer ;
std::string str = buf.str() ;

cout << "My string = " << str << endl ; 

But I find it tedious to create the stringstream variables all the time, especially if there are several of them in the same scope since you have to give each one a unique name. So I wrote the following macro:

#define MAKE_STRING( msg )  ( ((std::ostringstream&)(std::ostringstream() 
    << msg)).str() )

It creates a temporary stringstream object, streams in the message and returns the string buffer when it's done. Note that the cast is necessary since operator<<() returns a reference to an ostream but we need an ostringstream to get at the underlying string buffer. You would use it like this:

std::string str = MAKE_STRING( "The answer is: " << theAnswer ) ;

It becomes even handier when you only want a temporay string object e.g.

extern void processString( const std::string& ) ;

processString( MAKE_STRING( "The answer is: " << theAnswer ) ) ;

I also have a version that returns the C-style char* pointer if that's what you need:

#define MAKE_CSTRING( msg )  ( ((std::ostringstream&)
    (std::ostringstream() << msg)).str().c_str() )

char buf[ 1024 ] ; 
strcpy( buf , MAKE_CSTRING("The answer is: " << theAnswer) ) ;

The only thing you need to watch here is that the char* pointer returned is a pointer to a temporary object. So in the example above, it's OK since the string object will stay alive until the call to strcpy() returns. But this is wrong:

const char* pDontTryThisAtHome = MAKE_CSTRING( 
    "The answer is : " << theAnswer ) ; 
printf( "%s\n" , pDontTryThisAtHome ) ;

Conclusion

And that's it! Article count: 2.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Taka Muraoka

Other
Awasu
Australia Australia

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMemory address being printed Pinmemberan_phu8:32 28 Oct '04  
AnswerRe: Memory address being printed [modified] Pinmemberashkira22:27 5 Jul '09  
GeneralWord of Caution PinmemberMichael Geddes12:58 23 Dec '03  
GeneralRe: Word of Caution PinmemberJWood10:09 5 Sep '06  
The nice way around this is to get the standards committee to do something useful for once and include conversions in the std::string implementation. How difficult would it be to implement something for the basic data types?
 

A cynic is a man who, when he smells flowers, looks around for a coffin.
-H.L. Mencken


Generalwithout preprocessor... PinsussGoran Mitrovic12:18 18 Dec '02  
GeneralRe: without preprocessor... PinmemberTaka Muraoka12:31 18 Dec '02  
GeneralRe: without preprocessor... PinsussGoran Mitrovic12:59 18 Dec '02  
GeneralRe: without preprocessor... PinmemberTaka Muraoka13:00 18 Dec '02  
GeneralRe: without preprocessor... PinmemberWilliam E. Kempf6:57 19 Dec '02  
GeneralSTL beginner PinmemberPit M.2:14 18 Dec '02  
GeneralRe: STL beginner PinmemberTaka Muraoka2:20 18 Dec '02  
Generalstringstreams are cool PinmemberRyan S Roberts0:35 18 Dec '02  
GeneralRe: stringstreams are cool PinmemberTaka Muraoka0:45 18 Dec '02  
GeneralRe: stringstreams are cool PinmemberRyan S Roberts1:08 18 Dec '02  
GeneralRe: stringstreams are cool PinmemberDaniel Turini2:36 18 Dec '02  
GeneralRe: stringstreams are cool PinmemberTim Smith12:47 18 Dec '02  
GeneralRe: stringstreams are cool PinmemberTaka Muraoka12:55 18 Dec '02  
GeneralRe: stringstreams are cool PinmemberTim Smith14:29 18 Dec '02  
GeneralRe: stringstreams are cool PinmemberTaka Muraoka14:34 18 Dec '02  
GeneralRe: stringstreams are cool PinsussAlex S18:20 19 Dec '02  
GeneralRe: stringstreams are cool PinmemberDaniel Turini20:49 18 Dec '02  
GeneralRe: stringstreams are cool PinmemberWilliam E. Kempf7:08 19 Dec '02  
GeneralRe: stringstreams are cool PinmemberMichael Geddes12:42 23 Dec '03  
GeneralAh, the power of macros PinmemberMarc Clifton0:32 18 Dec '02  
GeneralRe: Ah, the power of macros PinmemberTaka Muraoka0:37 18 Dec '02  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120529.1 | Last Updated 17 Dec 2002
Article Copyright 2002 by Taka Muraoka
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid