basic::string Formatting

M

Mike Copeland

Is there a more convenient/simple way to specify string formatting
than the following?

ostringstream ossw;
string str;
long nts;
ossw.str(""), ossw << left << setw(12) << nts, str = ossw.str();

I have much of this type of code, with many setw() calls and various
right and left modifiers. It would be a lot simpler to have something
like "setwleft(12)" or something like that to reduce my code. (It might
execute faster, too...)
Of course, it was (to me) much simpler to use C-style formatting
(sprintf, etc.), but having to use ".c_str()" for string data is messy
and error-prone (when I forget to add it).
Are there other options for string formatting? Please advise. TIA
 
S

Stefan Ram

Is there a more convenient/simple way to specify string formatting
than the following?
ostringstream ossw;
string str;
long nts;
ossw.str(""), ossw << left << setw(12) << nts, str = ossw.str();
I have much of this type of code, with many setw() calls and various
right and left modifiers. It would be a lot simpler to have something
like "setwleft(12)" or something like that to reduce my code. (It might
execute faster, too...)

What's wrong with a function definition for repeating code?

::std::string printwleft( int const width, long const value )
{ ::std::eek:stringstream stream {};
stream << ::std::left << ::std::setw( width )<< value;
return stream.str(); }

You can also look at the more elaborate code in

From: Luca Risolia <[email protected]>
Newsgroups: comp.lang.c++
Subject: Re: printing in C++
Date: Thu, 14 Mar 2013 20:35:22 +0100
Message-ID: <[email protected]>

that shows how to create a custom manipulator.
 
J

James Kanze

On Sun, 30 Mar 2014 16:16:25 -0700 in comp.lang.c++, (e-mail address removed)
(Mike Copeland) wrote,
Something like
struct setwleft {
int width;
setwleft(int arg): width(arg) {}
};
std::eek:stream& operator<<(std::eek:stream &strm, setwleft const &saved)
{
strm << left << setw(saved.width);
return strm;
}
int main()
{
int nts=1009;
cout << "{" << setwleft(12) << nts << "}\n";
}
See Stroustrup section 21.4.6.3

This is the real solution, except that the manipulator will
typically be called something like "label", or whatever has
a semantic signification for the values on which it is used. In
practice, you should almost never use the standard manipulators
directly, because they specify the physical formatting, not the
logical.

Also, it's a nice touch for the manipulators to restore the
initial stream formatting in their destructor. This is a bit
tricky, since if more than one is used, their order of
construction (and thus their order of destruction) is not
specified, but it can be done in a base class, written once, and
then used for all of your manipulators.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top