Description: As in the c language printf("%3d",&a) is used for the format in which we want to print for this only the alternative is boost::format which is used here for the higher order numbers . boost::format is a class in which there is a string containing special characters to control formatting which is passed to the constructor of boost::function class.The data that replaces these special characters in the output is linked via the operator%.The format object is constructed from a format-string and is then given arguments through repeated calls to operator%.Each of these arguments are then converted to strings ,who are in turn combined into one string ,according to format-string
Syntax: General syntax of format string is:
boost::format(format string)%arg1,%arg2,....%argN
Directives in format string:
1.printf format string : In this %spec is used as the general syntax .the printf format supported boost.format follows the Unix98 open group printf precise syntax rather than the standard c language
The specification spec has the form :
[ N$ ] [ flags ] [ width ] [.precision] [argument-type] conversion-specifier. (any of the field any be optional)
> [ N$ ] : it is known as positional format specification .if it is not present then the argument will be taken one by one
> [flag] : flag is sequence of any of these:
> [width]: it specifies the minimal width of the string resulting from the conversion.
>[.precision]: it sets the stream precision .when output is the float type number,it sets the maximum number of digits after decimal point in fixed or scientific mood and when in default mood(eg: %g)
>[argument-type]: in printf family it is used to process the argument but in boost::format it is used with % operator which allows the template to carry the argument type.Argument type hh,h,l,ll,j,z and L are recognized as the Microsoft extensions w,I,i32,i64.
>conversion-specifier:
2.%|spec| : where spec is same as printf format specification as i explained in first point.
the pip delimited syntax is introduced to improve the readability of the format string but primarily to make the type conversion optional in spec
3.%N%: this is the simple positional notation request the formatting of the nth argument without any formatting option
Advantages:
1.safe : type-safe,and throws an exception ,for example the specification of too-many or too few arguments
2.extensible : work for any type that can be streamed
3.convenient: standard posix and similar format string
program:
#include <boost/format.hpp>
#include <iostream>
#include <iomanip>
#include <boost/cast.hpp>
#include <cassert>
using namespace std;
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;
using boost::io::str;
}
namespace MyNS_Manips {
using std::setfill;
using std::setw;
using std::hex ;
using std::dec ;
}
int main()
{
using namespace MyNS_ForOutput;
using namespace MyNS_Manips;
cout<<"these simple use of boost::format:"<<endl;
cout<<"normal use \n";
cout <<boost::format("(x,y) = (%1$+5d,%2$-5d ,%2$=5d) \n") % -23 % 35;
cout<<"\n use of different conversion specifier\n";
cout << boost::format("writing %s, x=%d : %d-th step \n") % "toto" % 40.23 % 50;
cout <<boost::format("_%1$4d_ is : _%1$#4x_ in hexadecimal, _%1$#4o_ in octal, and _%1 in string \n") % 18;
cout<<"\n use of %N% \n";
cout << boost::format("%1%.%2%.%3%") % 12 % 5 % 2014 << '\n';
cout<<"\n use of %|spec| \n";
cout << boost::format("%|1$1| %|2$20|") % "Hello" % 3 << std::endl;
cout<<"here below it is shown that same output can be there with different format writing"<<endl;
cout <<boost::format("(x,y) = (%+5d,%+5d) \n") % -23 % 35;
cout << boost::format("(x,y) = (%|+5|,%|+5|) \n") % -23 % 35;
cout << boost::format("(x,y) = (%|1$+5|,%|2$+5|) \n") % -23 % 35;
cout<<"\n use of manipulators \n";
cout <<boost::format("%2% %1% %2%\n") % 1 % group(setfill('X'), hex, setw(4), 16+3) ;
cout<<"\n use of assertion \n";
std::string s;
s= str( format(" %d %d ") % 11 % 22 );
assert( s == " 11 22 ");
cout << format("%%##%#x ") % 20 << endl;
cout<<"added features in printf"<<endl<<endl;
cout<<"same arguments can be used number of times \n";
cout << format("%1% %2% %3% %2% %1% \n") % "o" % "oo" % "O";
cout<<"\n use of vector\n";
vector<string> names(1, "sunita"),
surname(1,"konada"),
tel(1, "6745218890");
names.push_back("chaitanya");
surname.push_back("gunda sai");
tel.push_back("6777290123");
for( int i=0; i<names.size(); ++i)
cout << format("%1%, %2%, %|20t|%3%\n") % names[i] % surname[i] % tel[i];
cout << "\n";
cout<<"\n truncation of string \n";
cout << format("%|.2s| %|8c|.\n") % "root" % "user";
cout<<"\n nested output \n";
cout << format("%2$014x [%1%] %2$05s\n") % (format("%05s / %s") % -18 % 7)
% group(showbase, -100);
}
output:
these simple use of boost::format:
normal use
(x,y) = ( -23,35 , 35 )
use of different conversion specifier
writing toto, x=40.23 : 50-th step
_ 18_ is : _0x12_ in hexadecimal, _ 022_ in octal, and _18_ in string
use of %N%
12.5.2014
use of %|spec|
Hello 3
here below it is shown that same output can be there with different format writing
(x,y) = ( -23, +35)
(x,y) = ( -23, +35)
(x,y) = ( -23, +35)
use of manipulators
XX13 1 XX13
use of assertion
%##0x14
added features in printf
same arguments can be used number of times
o oo O oo o
use of vector
sunita, konada, 6745218890
chaitanya, gunda sai, 6777290123
truncation of string
ro u.
nested output
0x0000ffffff9c [-0018 / 7] -0100
Note: As we seen there non-positional format(%d) and positional format (%$4) and they should not be used together
// unsupported printf directives %n and asterisk-fields are purely ignored.
// do *NOT* provide an argument for them, it is an error.
cout << format("|%5d| %n") % 7 << endl;
// prints "| 7| "
cout << format("|%*.*d|") % 7 << endl;
// prints "|7|"
Comments