Brian Silverman | 8ed424f | 2018-08-04 23:36:27 -0700 | [diff] [blame^] | 1 | // ---------------------------------------------------------------------------- |
| 2 | // sample_new_features.cpp : demonstrate features added to printf's syntax |
| 3 | // ---------------------------------------------------------------------------- |
| 4 | |
| 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) |
| 8 | |
| 9 | // See http://www.boost.org/libs/format for library home page |
| 10 | |
| 11 | // ---------------------------------------------------------------------------- |
| 12 | |
| 13 | #include <iostream> |
| 14 | #include <iomanip> |
| 15 | |
| 16 | #include "boost/format.hpp" |
| 17 | |
| 18 | int main(){ |
| 19 | using namespace std; |
| 20 | using boost::format; |
| 21 | using boost::io::group; |
| 22 | |
| 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" |
| 27 | |
| 28 | |
| 29 | // ------------------------------------------------------------------------ |
| 30 | // Centered alignment : flag '=' |
| 31 | cout << format("_%|=6|_") % 1 << endl; |
| 32 | // prints "_ 1 _" : 3 spaces are padded before, and 2 after. |
| 33 | |
| 34 | |
| 35 | |
| 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"), |
| 42 | surname(1,"Durand"), |
| 43 | tel(1, "+33 (0) 123 456 789"); |
| 44 | |
| 45 | names.push_back("Jean"); |
| 46 | surname.push_back("de Lattre de Tassigny"); |
| 47 | tel.push_back("+33 (0) 987 654 321"); |
| 48 | |
| 49 | for(unsigned int i=0; i<names.size(); ++i) |
| 50 | cout << format("%1%, %2%, %|40t|%3%\n") % names[i] % surname[i] % tel[i]; |
| 51 | |
| 52 | /* prints : |
| 53 | |
| 54 | |
| 55 | Marc-François Michel, Durand, +33 (0) 123 456 789 |
| 56 | Jean, de Lattre de Tassigny, +33 (0) 987 654 321 |
| 57 | |
| 58 | |
| 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. |
| 62 | */ |
| 63 | |
| 64 | |
| 65 | |
| 66 | cerr << "\n\nEverything went OK, exiting. \n"; |
| 67 | return 0; |
| 68 | } |