A few boost::format examples

By , last updated October 28, 2019

In this article we will cover boost date and time format examples, horizontal alignment and formatting in loops. Other formatting examples: Numbers and wformat, boost String.

I always forget how to use boost::format when I need it. So, here are a few samples from when I did remember.

Only need is to include the proper boost header. No compile of any of the boost libraries is necessary. If you don’t know how to install Boost here is a quick guide for Windows with Visual Studio.

Basic usage


// build with make or:
//
// g++ -I../../boost_1_46_0 fmt.cpp
//

#include <iostream>
#include <sstream>
#include <boost/format.hpp>

using namespace std;
using namespace boost;

int main()
{
    stringstream ss;

    int year    = 2011;
    int month   = 3;
    int day     = 11;
    int hour    = 12;
    int minute  = 1;
    double seconds= 23.35;
    double epoch  = 1231234567890.35;

    // Fill with whitespace, right align
    ss << format("#1.0 %4i %4i %4in") % year % month % day;
    // Output: #1.0 2011    3   11

    // Fill with whitespace, left align
    ss << format("#1.1 %-4i %-4i %-4in") % year % month % day;
    // Output: #1.1 2011 3    11

    // Fill with zero (pad)
    ss << format("#2.0 %04i %04i %04in") % year % month % day;
    // Output: #2.0 2011 0003 0011

    // Floating point precision
    ss << format("#3.0 %11.2fn") % seconds;
    // Output: #3.0       23.35

    // Floating point precision with zero-fill (zero pad)
    ss << format("#4.0 %011.2fn") % seconds;
    // Output: #4.0 00000023.35

    // Left aligned string literal with 30+20 columns
    ss << format("#5.0 %-30s%-20sn") % "LEFT ALIGNED 1" % "LEFT ALIGNED 2";
    // Output: #5.0 LEFT ALIGNED 1                LEFT ALIGNED 2

    // Right aligned string literal
    ss << format("#6.0 %30s%20sn") % "RIGHT ALIGNED 1" % "RIGHT ALIGNED 2";
    // Output: #6.0                RIGHT ALIGNED 1     RIGHT ALIGNED 2

    cout << ss.str();

    return 0;
}

The output should be:

#1.0 2011    3   11
#1.1 2011 3    11
#2.0 2011 0003 0011
#3.0       23.35
#4.0 00000023.35
#5.0 LEFT ALIGNED 1                LEFT ALIGNED 2
#6.0                RIGHT ALIGNED 1     RIGHT ALIGNED 2

Read also: Boost::format String examples

Optimizing boost::format in loops

One of the things boost::format is good at, is using somewhat long time to produce the actual format object. This penalty can be mitigated and / or removed completely.

Bad example

WorldSave worldsave(false);

BOOST_FOREACH(const Entity_ptr & ent, entities)
{
    boost::format entfmt("Entity_%i"); // Bad bad bad position!
    std::string name = boost::str(entfmt % i++);
    worldsave.save(name, ent, datamap);
}

In this example, the boost::format is in the loop. This causes the format object to be constructed and destructed every iteration. This can be very expensive.

Good example

WorldSave worldsave(false);

boost::format entfmt("Entity_%i"); // Good position!

BOOST_FOREACH(const Entity_ptr & ent, entities)
{
    std::string name = boost::str(entfmt % i++);
    worldsave.save(name, ent, datamap);
}

In this example, the boost::format object is constructed outside of the loop. This is one of the most simple and most efficient optimizations you can do with boost::format.

Read also: 5 Great things about Boost

It is also possible to have the format object as a part of the class it is in. This way the format object is only constructed and destructed at class construction and destruction. However, boost::format IS NOT thread safe! If you’re doing threading, make sure the format object is encapsulated with each thread doing formatting.

The source is also available at Github (webclient).

Professional Software Developer, doing mostly C++. Connect with Kent on Twitter.