to_string-variadic.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #if 0
  2. g++ -Wall -Wextra -std=c++03 -I/usr/local/include/tr1_fwd -o "$0.run" "$0"
  3. exit
  4. #endif
  5. //#define CXXOMFORT_NOTICES 3
  6. #include <cxxomfort/library/string.hpp>
  7. #include <cxxomfort/string.hpp>
  8. #include <iostream>
  9. #include <iomanip>
  10. #include <ctime> // time
  11. #include <cstdlib>
  12. #include <ciso646> // for some old compilers
  13. int main () {
  14. using namespace std;
  15. //using std::to_string;
  16. //using cxxomfort::library::string::to_string; // variadic to_string
  17. namespace string2 = cxxomfort::library::string ;
  18. //using namespace cxxomfort::literals;
  19. cxxomfort::output_info(stdout);
  20. cout<< endl;
  21. cout<< "The following outputs should be: "<< endl;
  22. cout<< "1.- '15' via to_string(int): "<< endl;
  23. cout<< right<< setw(6)<< to_string(15)<< endl;
  24. cout<< "From now on we use variadic to_string:"<< endl;
  25. cout<< "2.- 'false' as we pass iomanips to to_string: "<< endl;
  26. cout<< string2::to_string(boolalpha, false)<< endl;
  27. cout<< "3.- '15.152000' or similar, via to_string(floating_point): "<< endl;
  28. cout<< to_string(15.152)<< endl;
  29. cout<< "4.- '003f' or similar, as we pass formatters to to_string: "<< endl;
  30. cout<< string2::to_string(setfill('0'), setw(4), hex, 0x3fU)<< endl;
  31. cout<< "4.- the same as above but we're using normal << : "<< endl;
  32. cout<< setfill('0') << setw(4)<< hex<< 0x3fU << endl;
  33. cout<< endl;
  34. cout<<
  35. "The output of the following section is the current system time, \n"
  36. "with a Daytime/Nighttime suffix and right-aligned at a 12-characters width.\n"<< endl;
  37. time_t tt(time(0));
  38. struct tm tmdata= *localtime(&tt);
  39. int hours = tmdata.tm_hour, minutes = tmdata.tm_min;
  40. bool daytime = (hours >= 7) and (hours<= 18);
  41. cout << right << setfill('_') << setw(12)
  42. << string2::to_string(hours, ':', setfill('0'), setw(2), minutes, " ", (daytime?"D":"N"))
  43. << endl;
  44. cout<< "123456789012345"<< endl;
  45. }