1 // ----------------------------------------------------------------------------
2 // sample_new_features.cpp : demonstrate features added to printf's syntax
3 // ----------------------------------------------------------------------------
5 // Copyright Samuel Krempp 2003. Use, modification, and distribution are
6 // subject to the Boost Software License, Version 1.0. (See accompanying
7 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9 // See http://www.boost.org/libs/format for library home page
11 // ----------------------------------------------------------------------------
16 #include "boost/format.hpp"
21 using boost::io::group
;
23 // ------------------------------------------------------------------------
24 // Simple style of reordering :
25 cout
<< format("%1% %2% %3% %2% %1% \n") % "o" % "oo" % "O";
26 // prints "o oo O oo o \n"
29 // ------------------------------------------------------------------------
30 // Centered alignment : flag '='
31 cout
<< format("_%|=6|_") % 1 << endl
;
32 // prints "_ 1 _" : 3 spaces are padded before, and 2 after.
36 // ------------------------------------------------------------------------
37 // Tabulations : "%|Nt|" => tabulation of N spaces.
38 // "%|NTf|" => tabulation of N times the character <f>.
39 // are useful when printing lines with several fields whose width can vary a lot
40 // but we'd like to print some fields at the same place when possible :
41 vector
<string
> names(1, "Marc-François Michel"),
43 tel(1, "+33 (0) 123 456 789");
45 names
.push_back("Jean");
46 surname
.push_back("de Lattre de Tassigny");
47 tel
.push_back("+33 (0) 987 654 321");
49 for(unsigned int i
=0; i
<names
.size(); ++i
)
50 cout
<< format("%1%, %2%, %|40t|%3%\n") % names
[i
] % surname
[i
] % tel
[i
];
55 Marc-François Michel, Durand, +33 (0) 123 456 789
56 Jean, de Lattre de Tassigny, +33 (0) 987 654 321
59 the same using width on each field lead to unnecessary too long lines,
60 while 'Tabulations' insure a lower bound on the *sum* of widths,
61 and that's often what we really want.
66 cerr
<< "\n\nEverything went OK, exiting. \n";