Boost logo

Boost Users :

Subject: Re: [Boost-users] Why is boost::format::operator% not declared const?
From: Jeff Flinn (Jeffrey.Flinn_at_[hidden])
Date: 2013-01-16 08:33:51


On 1/16/2013 2:47 AM, Chris Stankevitz wrote:
> On Tue, Jan 15, 2013 at 5:49 PM, Steve Lorimer <steve.lorimer_at_[hidden]> wrote:
>> Think about what happens if you feed multiple elements via operator% and
>> then call .str() - you get a std::string back - it has to store everything
>> you've fed in internally so that you can access the formatted string later
>> via .str()
>
> Steve,
>
> Thank you. I never used boost::format::str, but I agree that its
> presence indicates that operator% must alter an internal state.
>
> Question 1: Is there an efficient way to do the following (and by
> "more efficient" I mean fewer format constructors and string parsing):
>
> void WriteLog(float x, float y, float z, const Employee& e)
> {
> std::cout
> << "Values: "
> << boost::format("%0.3") % x
> << e
> << boost::format("%0.3") % y
> << boost::format("%0.3") % z;
> }
>
> Question 2: Is there a more efficient way to do this:
>
> std::string FloatToString(float x, int DigitsAfterDecimal)
> {
> std::ostringstream FormatStream;
>
> FormatStream << "%0." << DigitsAfterDecimal;
>
> boost::format Formatter(FormatStream.str());
>
> Formatter % x;
>
> return Formatter.str();
> }

Have you looked at boost spirit's karma lib? IIRC, it outperforms most
other libs with formatted output like this.

Jeff


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net