Click here to Skip to main content
15,867,686 members
Articles / Mobile Apps / Windows Mobile

Quick and convenient way to build STL strings.

Rate me:
Please Sign up or sign in to vote.
4.00/5 (17 votes)
9 Jul 2013CPOL1 min read 117.2K   22   28
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Awasu
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
tnagata205010-Jul-13 14:35
tnagata205010-Jul-13 14:35 
GeneralMemory address being printed Pin
an_phu28-Oct-04 8:32
an_phu28-Oct-04 8:32 
AnswerRe: Memory address being printed [modified] Pin
ashkira5-Jul-09 22:27
ashkira5-Jul-09 22:27 
GeneralWord of Caution Pin
Michael Geddes23-Dec-03 12:58
Michael Geddes23-Dec-03 12:58 
GeneralRe: Word of Caution Pin
JWood5-Sep-06 10:09
JWood5-Sep-06 10:09 
Generalwithout preprocessor... Pin
Goran Mitrovic18-Dec-02 12:18
Goran Mitrovic18-Dec-02 12:18 
GeneralRe: without preprocessor... Pin
Taka Muraoka18-Dec-02 12:31
Taka Muraoka18-Dec-02 12:31 
GeneralRe: without preprocessor... Pin
Goran Mitrovic18-Dec-02 12:59
Goran Mitrovic18-Dec-02 12:59 
GeneralRe: without preprocessor... Pin
Taka Muraoka18-Dec-02 13:00
Taka Muraoka18-Dec-02 13:00 
GeneralRe: without preprocessor... Pin
William E. Kempf19-Dec-02 6:57
William E. Kempf19-Dec-02 6:57 
GeneralRe: without preprocessor... Pin
Nik Colman10-Oct-13 5:51
Nik Colman10-Oct-13 5:51 
GeneralSTL beginner Pin
Pit M.18-Dec-02 2:14
Pit M.18-Dec-02 2:14 
GeneralRe: STL beginner Pin
Taka Muraoka18-Dec-02 2:20
Taka Muraoka18-Dec-02 2:20 
Generalstringstreams are cool Pin
Ryan_Roberts18-Dec-02 0:35
Ryan_Roberts18-Dec-02 0:35 
GeneralRe: stringstreams are cool Pin
Taka Muraoka18-Dec-02 0:45
Taka Muraoka18-Dec-02 0:45 
GeneralRe: stringstreams are cool Pin
Ryan_Roberts18-Dec-02 1:08
Ryan_Roberts18-Dec-02 1:08 
GeneralRe: stringstreams are cool Pin
Daniel Turini18-Dec-02 2:36
Daniel Turini18-Dec-02 2:36 
GeneralRe: stringstreams are cool Pin
Tim Smith18-Dec-02 12:47
Tim Smith18-Dec-02 12:47 
GeneralRe: stringstreams are cool Pin
Taka Muraoka18-Dec-02 12:55
Taka Muraoka18-Dec-02 12:55 
GeneralRe: stringstreams are cool Pin
Tim Smith18-Dec-02 14:29
Tim Smith18-Dec-02 14:29 
GeneralRe: stringstreams are cool Pin
Taka Muraoka18-Dec-02 14:34
Taka Muraoka18-Dec-02 14:34 
GeneralRe: stringstreams are cool Pin
Alex S19-Dec-02 18:20
Alex S19-Dec-02 18:20 
GeneralRe: stringstreams are cool Pin
Daniel Turini18-Dec-02 20:49
Daniel Turini18-Dec-02 20:49 
GeneralRe: stringstreams are cool Pin
William E. Kempf19-Dec-02 7:08
William E. Kempf19-Dec-02 7:08 
GeneralRe: stringstreams are cool Pin
Michael Geddes23-Dec-03 12:42
Michael Geddes23-Dec-03 12:42 
I've made my own similar class (though it doesn't handle locales) - which I did after seeing % used in this manner in python... and it handles detecting mismatched string types.

Both these methods have their place. ostringstreams are good for creating streams of data... constructing queryies in other languages (SQL for eg)... the sprintf format has its own uses.

One important thing to remember is that internationalised strings with multiple place-holders need to be allowed to order the place holders differently.
Neither ostringstream or pure sprintf formats allow this. I believe boosts format does, as well as microsoft's FormatMessage - which uses %1 %2 %3!2.2d! instead of %s %s %2.2d.
I chose to implement this version (as well as sprintf), partly 'cause we were already using microsoft's version anyway.





//.ichael G

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

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