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
6// The main purpose of this example is to show how a single container type can
7// be formatted using different output grammars.
8
9#include <boost/spirit/include/karma.hpp>
10#include <boost/spirit/include/karma_stream.hpp>
11
12#include <iostream>
13#include <vector>
14#include <algorithm>
15#include <cstdlib>
16
17using namespace boost::spirit;
18using namespace boost::spirit::ascii;
19
20///////////////////////////////////////////////////////////////////////////////
21int main()
22{
23 ///////////////////////////////////////////////////////////////////////////
24 // vector
25 std::vector<int> v (8);
26 std::generate(first: v.begin(), last: v.end(), gen: std::rand); // randomly fill the vector
27
28 std::cout << "Output 8 integers from a std::vector<int>..." << std::endl;
29
30 // output the container as a sequence without any separation
31 std::cout << "...without any separation" << std::endl;
32 std::cout <<
33 karma::format(
34 expr: *int_, // format description
35 attr: v // data
36 ) << std::endl << std::endl;
37
38 // output the container as a space separated sequence
39 std::cout << "...as space delimited list" << std::endl;
40 std::cout <<
41 karma::format_delimited(
42 xpr: *int_, // format description
43 d: space, // delimiter
44 attr: v // data
45 ) << std::endl << std::endl;
46
47 std::cout <<
48 karma::format_delimited(
49 xpr: '[' << *int_ << ']', // format description
50 d: space, // delimiter
51 attr: v // data
52 ) << std::endl << std::endl;
53
54 // output the container as a comma separated list
55 std::cout << "...as comma separated list" << std::endl;
56 std::cout <<
57 karma::format(
58 expr: int_ % ", ", // format description
59 attr: v // data
60 ) << std::endl << std::endl;
61
62 std::cout <<
63 karma::format(
64 expr: '[' << (int_ % ", ") << ']', // format description
65 attr: v // data
66 ) << std::endl << std::endl;
67
68 // output the container as a comma separated list of double's
69 std::cout << "...as comma separated list of doubles" << std::endl;
70 std::cout <<
71 karma::format(
72 expr: double_ % ", ", // format description
73 attr: v // data
74 ) << std::endl << std::endl;
75
76 // output the container as a comma separated list of items enclosed in '()'
77 std::cout << "..as list of ints enclosed in '()'" << std::endl;
78 std::cout <<
79 karma::format(
80 expr: ('(' << int_ << ')') % ", ", // format description
81 attr: v // data
82 ) << std::endl << std::endl;
83
84 std::cout <<
85 karma::format(
86 expr: '[' << (
87 ('(' << int_ << ')') % ", "
88 ) << ']', // format description
89 attr: v // data
90 ) << std::endl << std::endl;
91
92 // output the container as a HTML list
93 std::cout << "...as HTML bullet list" << std::endl;
94 std::cout <<
95 karma::format_delimited(
96 xpr: "<ol>" <<
97 // no delimiting within verbatim
98 *verbatim[" <li>" << int_ << "</li>"]
99 << "</ol>", // format description
100 d: '\n', // delimiter
101 attr: v // data
102 ) << std::endl;
103
104 // output the container as right aligned column
105 std::cout << "...right aligned in a column" << std::endl;
106 std::cout <<
107 karma::format_delimited(
108 xpr: *verbatim[
109 "|" << right_align[int_] << "|"
110 ], // format description
111 d: '\n', // delimiter
112 attr: v // data
113 ) << std::endl;
114
115 std::cout << std::endl;
116 return 0;
117}
118
119

source code of boost/libs/spirit/example/karma/quick_start1.cpp