sample_new_features.cpp 2.3 KB

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