quick_start1.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // Copyright (c) 2001-2010 Hartmut Kaiser
  2. //
  3. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. // The main purpose of this example is to show how a single container type can
  6. // be formatted using different output grammars.
  7. #include <boost/config/warning_disable.hpp>
  8. #include <boost/spirit/include/karma.hpp>
  9. #include <boost/spirit/include/karma_stream.hpp>
  10. #include <iostream>
  11. #include <vector>
  12. #include <algorithm>
  13. #include <cstdlib>
  14. using namespace boost::spirit;
  15. using namespace boost::spirit::ascii;
  16. ///////////////////////////////////////////////////////////////////////////////
  17. int main()
  18. {
  19. ///////////////////////////////////////////////////////////////////////////
  20. // vector
  21. std::vector<int> v (8);
  22. std::generate(v.begin(), v.end(), std::rand); // randomly fill the vector
  23. std::cout << "Output 8 integers from a std::vector<int>..." << std::endl;
  24. // output the container as a sequence without any separation
  25. std::cout << "...without any separation" << std::endl;
  26. std::cout <<
  27. karma::format(
  28. *int_, // format description
  29. v // data
  30. ) << std::endl << std::endl;
  31. // output the container as a space separated sequence
  32. std::cout << "...as space delimited list" << std::endl;
  33. std::cout <<
  34. karma::format_delimited(
  35. *int_, // format description
  36. space, // delimiter
  37. v // data
  38. ) << std::endl << std::endl;
  39. std::cout <<
  40. karma::format_delimited(
  41. '[' << *int_ << ']', // format description
  42. space, // delimiter
  43. v // data
  44. ) << std::endl << std::endl;
  45. // output the container as a comma separated list
  46. std::cout << "...as comma separated list" << std::endl;
  47. std::cout <<
  48. karma::format(
  49. int_ % ", ", // format description
  50. v // data
  51. ) << std::endl << std::endl;
  52. std::cout <<
  53. karma::format(
  54. '[' << (int_ % ", ") << ']', // format description
  55. v // data
  56. ) << std::endl << std::endl;
  57. // output the container as a comma separated list of double's
  58. std::cout << "...as comma separated list of doubles" << std::endl;
  59. std::cout <<
  60. karma::format(
  61. double_ % ", ", // format description
  62. v // data
  63. ) << std::endl << std::endl;
  64. // output the container as a comma separated list of items enclosed in '()'
  65. std::cout << "..as list of ints enclosed in '()'" << std::endl;
  66. std::cout <<
  67. karma::format(
  68. ('(' << int_ << ')') % ", ", // format description
  69. v // data
  70. ) << std::endl << std::endl;
  71. std::cout <<
  72. karma::format(
  73. '[' << (
  74. ('(' << int_ << ')') % ", "
  75. ) << ']', // format description
  76. v // data
  77. ) << std::endl << std::endl;
  78. // output the container as a HTML list
  79. std::cout << "...as HTML bullet list" << std::endl;
  80. std::cout <<
  81. karma::format_delimited(
  82. "<ol>" <<
  83. // no delimiting within verbatim
  84. *verbatim[" <li>" << int_ << "</li>"]
  85. << "</ol>", // format description
  86. '\n', // delimiter
  87. v // data
  88. ) << std::endl;
  89. // output the container as right aligned column
  90. std::cout << "...right aligned in a column" << std::endl;
  91. std::cout <<
  92. karma::format_delimited(
  93. *verbatim[
  94. "|" << right_align[int_] << "|"
  95. ], // format description
  96. '\n', // delimiter
  97. v // data
  98. ) << std::endl;
  99. std::cout << std::endl;
  100. return 0;
  101. }