# Include <iostream>
# Include <iomanip>
# Include <cassert>
# Include "boost/format. hpp"
// 2 custom namespaces, to bring in a few useful names:
Namespace MyNS_ForOutput {
Using std: cout; using std: cerr;
Using std: string;
Using std: endl; using std: flush;
Using boost: format;
Using boost: io: group;
}
Namespace MyNS_Manips {
Using std: setfill;
Using std: setw;
Using std: hex;
Using std: dec;
// Gcc-2.95 doesnt define the next ones
// Using std: showbase;
// Using std: left;
// Using std: right;
// Using std: internal;
}
Int main (){
Using namespace myns_foroutput;
Using namespace myns_manips;
STD: cout <format ("% | 1 $1 | % | 2 $3 |") % "hello" % 3 <STD: Endl;
// Reordering:
Cout <format ("% 1% % 2% % 3% % 2% % 1%/N") % "O" % "oo" % "O"; // 'simple' style.
// Prints "O oo o/N"
Cout <format ("(x, y) = (% 1 $ + 5D, % 2 $ + 5D)/n") %-23% 35; // POSIX-printf Style
// No reordering:
Cout <format ("Writing % s, x = % s: % d-th STEP/N") % "Toto" % 40.23% 50;
// Prints "writing toto, x = 40.23: 50-th step/n"
Cout <format ("(x, y) = (% + 5d, % + 5d)/n") %-23% 35;
Cout <format ("(x, y) = (% | + 5 |, % | + 5 |)/n") %-23% 35;
Cout <format ("(x, y) = (% | 1 $ + 5 |, % | 2 $ + 5 |)/n") %-23% 35;
// All those are the same, it prints "(x, y) = (-23, + 35)/n"
// Using manipulators, via 'group ':
Cout <format ("% 2% % 1% % 2%/n") % 1% group (setfill ('x'), hex, setw (4), 16 + 3 );
// Prints "XX13 1 XX13/n"
// Printf directives's type-flag can be used to pass formatting options:
Cout <format ("_ % 1 $ 4D _ is: _ % 1 $ # 4x _, _ % 1 $ # 4O _, and _ % 1 $ S _ by default/N ") % 18;
// Prints "_ 18 _ is: _ 0x12 _, _ 022 _, and _ 18 _ by default/N"
// Taking the string value:
STD: String S;
S = STR (format ("% d") % 11% 22 );
Assert (S = "11 22 ");
//-----------------------------------------------
// % Prints '%'
Cout <format ("% ##%# X") % 20 <Endl;
// Prints "% #0x14"
//-----------------------------------------------
// Enforcing the right number of arguments
// Too much arguments will throw an exception when feeding the unwanted argument:
Try {
Format ("% 1% % 1%") % 101% 102;
// The format-string refers to ONE argument, twice. not 2 arguments.
// Thus giving 2 arguments is an error
}
Catch (boost: io: too_many_args & exc ){
Cerr <exc. what () </n/t *** Dont worry, that was planned/n ";
}
// Too few arguments when requesting the result will also throw an exception:
Try {
Cerr <format ("% | 3 $ |") % 101;
// Even if % 1 $ and % 2 $ are not used, you shoshould have given 3 arguments
}
Catch (boost: io: too_few_args & exc ){
Cerr <exc. what () </n/t *** Dont worry, that was planned/n ";
}
Cerr <"/n/nEverything went OK, exiting./n ";
Return 0;
}
Output:
Hello 3
O oo O oo o
(X, y) = (-23, + 35)
Writing toto, x = 40.23: 50-th step
(X, y) = (-23, + 35)
(X, y) = (-23, + 35)
(X, y) = (-23, + 35)
XX13 1 XX13
_ 18 _ is: _ 0x12 _, _ 022 _, and _ 18 _ by default
% #0x14
Boost: too_many_args: format-string refered to less arguments than were passed
* ** Dont worry, that was planned
Boost: too_few_args: format-string refered to more arguments than were passed
* ** Dont worry, that was planned
Everything went OK, exiting.