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

Quick and convenient way to build STL strings.

By Taka Muraoka | 16 Dec 2002
A quick and convenient way to build STL strings.
2 votes, 13.3%
1
1 vote, 6.7%
2
3 votes, 20.0%
3
3 votes, 20.0%
4
6 votes, 40.0%
5
3.88/5 - 15 votes
2 removed
μ 3.74, σa 2.53 [?]

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_phu9:32 28 Oct '04  
AnswerRe: Memory address being printed [modified] Pinmemberashkira23:27 5 Jul '09  
GeneralWord of Caution PinmemberMichael Geddes13:58 23 Dec '03  
GeneralRe: Word of Caution PinmemberJWood11:09 5 Sep '06  
Generalwithout preprocessor... PinsussGoran Mitrovic13:18 18 Dec '02  
GeneralRe: without preprocessor... PinmemberTaka Muraoka13:31 18 Dec '02  
GeneralRe: without preprocessor... PinsussGoran Mitrovic13:59 18 Dec '02  
GeneralRe: without preprocessor... PinmemberTaka Muraoka14:00 18 Dec '02  
GeneralRe: without preprocessor... PinmemberWilliam E. Kempf7:57 19 Dec '02  
GeneralSTL beginner PinmemberPit M.3:14 18 Dec '02  
GeneralRe: STL beginner PinmemberTaka Muraoka3:20 18 Dec '02  
Generalstringstreams are cool PinmemberRyan S Roberts1:35 18 Dec '02  
GeneralRe: stringstreams are cool PinmemberTaka Muraoka1:45 18 Dec '02  
GeneralRe: stringstreams are cool PinmemberRyan S Roberts2:08 18 Dec '02  
GeneralRe: stringstreams are cool PinmemberDaniel Turini3:36 18 Dec '02  
GeneralRe: stringstreams are cool PinmemberTim Smith13:47 18 Dec '02  
GeneralRe: stringstreams are cool PinmemberTaka Muraoka13:55 18 Dec '02  
GeneralRe: stringstreams are cool PinmemberTim Smith15:29 18 Dec '02  
GeneralRe: stringstreams are cool PinmemberTaka Muraoka15:34 18 Dec '02  
GeneralRe: stringstreams are cool PinsussAlex S19:20 19 Dec '02  
GeneralRe: stringstreams are cool PinmemberDaniel Turini21:49 18 Dec '02  
GeneralRe: stringstreams are cool PinmemberWilliam E. Kempf8:08 19 Dec '02  
GeneralRe: stringstreams are cool PinmemberMichael Geddes13:42 23 Dec '03  
GeneralAh, the power of macros PinmemberMarc Clifton1:32 18 Dec '02  
GeneralRe: Ah, the power of macros PinmemberTaka Muraoka1: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
Web03 | 2.5.120210.1 | Last Updated 17 Dec 2002
Article Copyright 2002 by Taka Muraoka
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid