Click here to Skip to main content
11,495,486 members (64,318 online)
Click here to Skip to main content

Quick and convenient way to build STL strings.

, 9 Jul 2013 CPOL 95.1K 22
Rate this:
Please Sign up or sign in to vote.
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 Smile | :)

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)

Share

About the Author

Taka Muraoka
Awasu
Australia Australia
No Biography provided

Comments and Discussions

 
GeneralMy vote of 5 Pin
Member 936038010-Jul-13 15:35
memberMember 936038010-Jul-13 15:35 
GeneralMemory address being printed Pin
an_phu28-Oct-04 9:32
memberan_phu28-Oct-04 9:32 
AnswerRe: Memory address being printed [modified] Pin
ashkira5-Jul-09 23:27
memberashkira5-Jul-09 23:27 
GeneralWord of Caution Pin
Michael Geddes23-Dec-03 13:58
memberMichael Geddes23-Dec-03 13:58 
GeneralRe: Word of Caution Pin
JWood5-Sep-06 11:09
memberJWood5-Sep-06 11:09 
Generalwithout preprocessor... Pin
Goran Mitrovic18-Dec-02 13:18
sussGoran Mitrovic18-Dec-02 13:18 
GeneralRe: without preprocessor... Pin
Taka Muraoka18-Dec-02 13:31
memberTaka Muraoka18-Dec-02 13:31 
GeneralRe: without preprocessor... Pin
Goran Mitrovic18-Dec-02 13:59
sussGoran Mitrovic18-Dec-02 13:59 
GeneralRe: without preprocessor... Pin
Taka Muraoka18-Dec-02 14:00
memberTaka Muraoka18-Dec-02 14:00 
GeneralRe: without preprocessor... Pin
William E. Kempf19-Dec-02 7:57
memberWilliam E. Kempf19-Dec-02 7:57 
GeneralRe: without preprocessor... Pin
Nik Colman10-Oct-13 6:51
memberNik Colman10-Oct-13 6:51 
GeneralSTL beginner Pin
Pit M.18-Dec-02 3:14
memberPit M.18-Dec-02 3:14 
GeneralRe: STL beginner Pin
Taka Muraoka18-Dec-02 3:20
memberTaka Muraoka18-Dec-02 3:20 
Generalstringstreams are cool Pin
Ryan S Roberts18-Dec-02 1:35
memberRyan S Roberts18-Dec-02 1:35 
GeneralRe: stringstreams are cool Pin
Taka Muraoka18-Dec-02 1:45
memberTaka Muraoka18-Dec-02 1:45 
GeneralRe: stringstreams are cool Pin
Ryan S Roberts18-Dec-02 2:08
memberRyan S Roberts18-Dec-02 2:08 
GeneralRe: stringstreams are cool Pin
Daniel Turini18-Dec-02 3:36
memberDaniel Turini18-Dec-02 3:36 
GeneralRe: stringstreams are cool Pin
Tim Smith18-Dec-02 13:47
memberTim Smith18-Dec-02 13:47 
GeneralRe: stringstreams are cool Pin
Taka Muraoka18-Dec-02 13:55
memberTaka Muraoka18-Dec-02 13:55 
GeneralRe: stringstreams are cool Pin
Tim Smith18-Dec-02 15:29
memberTim Smith18-Dec-02 15:29 
GeneralRe: stringstreams are cool Pin
Taka Muraoka18-Dec-02 15:34
memberTaka Muraoka18-Dec-02 15:34 
GeneralRe: stringstreams are cool Pin
Alex S19-Dec-02 19:20
sussAlex S19-Dec-02 19:20 
GeneralRe: stringstreams are cool Pin
Daniel Turini18-Dec-02 21:49
memberDaniel Turini18-Dec-02 21:49 
GeneralRe: stringstreams are cool Pin
William E. Kempf19-Dec-02 8:08
memberWilliam E. Kempf19-Dec-02 8:08 
GeneralRe: stringstreams are cool Pin
Michael Geddes23-Dec-03 13:42
memberMichael Geddes23-Dec-03 13:42 
GeneralAh, the power of macros Pin
Marc Clifton18-Dec-02 1:32
memberMarc Clifton18-Dec-02 1:32 
GeneralRe: Ah, the power of macros Pin
Taka Muraoka18-Dec-02 1:37
memberTaka Muraoka18-Dec-02 1:37 
GeneralRe: Ah, the power of macros Pin
Marc Clifton18-Dec-02 1:56
memberMarc Clifton18-Dec-02 1:56 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150520.1 | Last Updated 9 Jul 2013
Article Copyright 2002 by Taka Muraoka
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid