vectorvector with Line-Breaks (Part I)vector with Line-Breaks (Part II)vector of vectorsvector of lists of pairss as XMLmapThe following series of examples should tell you all you need to know to begin using Format Lite. To compensate for the lack of simple initialization syntax in C++, the examples make use of pseudocode, as in the following:
list<string> strings = { "London", "Paris", "Prague" };
The regression tests and the example programs in the directory <libs/format_lite/example> address this limitation by using Boost.Assign.
For additional examples, see Source: This example formats a Output: Source: This example formats a Output: Because no indentation increment was specified explicitly, the above program used the default indentation of a single tab character. If instead we wanted the This would result in the following output: Similarly, if we wanted the Source: This example formats a Output: Source: This example formats a Output: Source: This example formats a Output: Source: This example formats a Output: Source: In general, data structures written using [To do: add more detailed description.] For example, Output: Revised
20 May, 2004
© Copyright Jonathan Turkanis, 2004
Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
Sha'arei Tefila, an Orthodox Shul (Synagogue) in Salt Lake City, Utah Chabad Lubavitch of Utah
Sha'arei Tefila, an Orthodox Shul (Synagogue) in Salt Lake City, Utah Chabad Lubavitch of Utah
punctuate.
Example 1: Printing a
vector<libs/format_lite/example/example1.cpp>vector of strings using a punctuation sequence specified with the function template punctuate.#include <iostream>
#include <string>
#include <vector>
#include <boost/iostreams/format_lite.hpp> // punctuate and operator<<
int main()
{
using namespace std;
using namespace boost::io;
// pseudocode initialization
vector<string> strings = { "London", "Paris", "Prague" };
cout << punctuate< vector<string> >("[ ", ", ", " ]")
<< strings;
}
[ London, Paris, Prague ]
Example 2: Printing a
vector with Line-breaks (part I)<libs/format_lite/example/example2a.cpp>vector of strings using a punctuation sequence and options for line breaks and indentation specified with punctuate.#include <iostream>
#include <string>
#include <vector>
#include <boost/iostreams/format_lite.hpp> // punctuate and operator<<
int main()
{
using namespace std;
using namespace boost::io;
// pseudocode initialization
vector<string> strings = { "London", "Paris", "Prague" };
int flags = punc::break_after_open |
punc::indent_after_open |
punc::break_after_sep |
punc::break_before_close;
cout << punctuate< vector<string> >("[", ",", "]", flags)
<< strings;
}
[
London,
Paris,
Prague
]
vector's elements to be indented by three spaces, we could use the following punctuation flags: int flags = punc::break_after_open |
punc::indent_after_open |
punc::break_after_sep |
punc::break_before_close |
(punc::spaces * 3);
[
London,
Paris,
Prague
]
vector's elements to be indented by two tabs, we could use the following punctuation flags:>
int flags = punc::break_after_open |
punc::indent_after_open |
punc::break_after_sep |
punc::break_before_close |
(punc::tabs * 2);
Finally, when tabs and spaces are specified together, as follows,
int flags = punc::break_after_open |
punc::indent_after_open |
punc::break_after_sep |
punc::break_before_close |
(punc::spaces * 3) |
(punc::tabs * 2);
the tabs are inserted before the spaces.
Example 3: Printing a
vector with Line-breaks (part II)<libs/format_lite/example/example3.cpp>vector of strings using a different convention for line-breaks.#include <iostream>
#include <string>
#include <vector>
#include <boost/iostreams/format_lite.hpp> // punctuate and operator<<
int main()
{
using namespace std;
using namespace boost::io;
// pseudocode initialization
vector<string> strings = { "London", "Paris", "Prague" };
int flags = punc::indent_after_open |
punc::break_before_sep |
(punc::space * 2);
cout << punctuate< vector<string> >("[ ", ", ", " ]", flags)
<< strings;
}
[ London
, Paris
, Prague ]
Example 4: Printing a
vector of vectors<libs/format_lite/example/example4.cpp>vector of vectors of strings. Note that we use the selector vector<_> to specify the punctuation sequence for the outer vector and the selector vector<string> to specify the punctuation sequence for the inner vector. (See punctuate for an explanation of selectors.)#include <iostream>
#include <string>
#include <vector>
#include <boost/iostreams/format_lite.hpp> // punctuate and operator<<
int main()
{
using namespace std;
using namespace boost::io;
// pseudocode initialization
vector< vector<string> > vectors =
{ { "London", "Paris", "Prague" },
{ "Rain", "Snow", "Fog" },
{ "Cat", "Rabbit", "Dog" } };
int flags = punc::indent_after_open |
punc::break_after_sep |
(punc::spaces * 2);
cout << punctuate< vector<_> >("[ ", ",", " ]", flags)
<< punctuate< vector<string> >("{ ", ", ", " }")
<< vectors;
}
[ { London, Paris, Prague },
{ Rain, Snow, Fog },
{ Cat, Rabbit, Dog } ]
Example 5: Printing a
vector of lists of pairs as XML<libs/format_lite/example/example5.cpp>vector of lists of pairs as XML. This is the first example whose output will not be read back correctly using operator>>, since it uses separator sequences containing no non-whitespace characters. (See Inputting Data Structures.)#include <iostream>
#include <list>
#include <string>
#include <vector>
#include <boost/iostreams/format_lite.hpp> // punctuate and operator<<
int main()
{
using namespace std;
using namespace boost::io;
// pseudocode initialization
vector< list< pair<string, string> > > lists =
{ { { "London", "England"}, { "Paris", "France"} },
{ { "Sofa", "Living Room"}, { "Stove", "Kitchen"} },
{ { "Brain", "Skull"}, { "Appendix", "Abdomen"} } };
int flags = punc::break_after_open |
punc::indent_after_open |
punc::break_after_sep |
(punc::spaces * 4);
cout << punctuate< vector<_> >("<vector>", "", "</vector>", flags)
<< punctuate< list<_> >("<list>", "", "</list>", flags)
<< punctuate< pair<_,_> >("<pair first='", "' second='", "'/>")
<< lists;
}
<vector>
<list>
<pair first='London' second='England'/>
<pair first='Paris' second='France'/>
</list>
<list>
<pair first='Sofa' second='Living Room'/>
<pair first='Stove' second='Kitchen'/>
</list>
<list>
<pair first='Brain' second='Skull'/>
<pair first='Appendix' second='Abdomen'/>
</list>
</vector>
Example 6: Printing a
map<libs/format_lite/example/example6.cpp>map<string, string>. Since the value type of map is a specialization of pair, we can specify how key-value pairs are formatted by using a pair as a selector.#include <iostream>
#include <map>
#include <string>
#include <boost/iostreams/format_lite.hpp> // punctuate and operator<<
int main()
{
using namespace std;
using namespace boost::io;
// pseudocode initialization
map< string, string > cities =
{ { "London", "England"},
{ "Paris", "France"},
{ "Moscow", "Russia"} };
cout << punctuate< map<_,_> >("{ ", ", ", " }")
<< punctuate< pair<_,_> >("(", ":", ")")
<< cities;
}
{ (London:England), (Moscow:Russia), (Paris:France) }
Inputting Data Structures
<libs/format_lite/example/example7.cpp>operator<< can be read back using operator>> with the same punctuation sequences. These restrictions apply:
std::basic_string are given special treatment: instead of extracting them using operator>>, they are read character by character until an appropriate punctuation sequence is encountered.#include <iostream>
#include <map>
#include <sstream>
#include <string>
#include <boost/iostreams/format_lite.hpp> // punctuate and operator<<
int main()
{
using namespace std;
using namespace boost::io;
map< string, string > cities;
istringstream in("{ (London:England), (Paris:France), (Moscow:Russia) }");
in >> punctuate< map<_,_> >("{ ", ", ", " }")
>> punctuate< pair<_,_> >("(", ":", ")")
>> cities;
cout << punctuate< map<_,_> >("{ ", ", ", " }")
<< punctuate< pair<_,_> >("(", ":", ")")
<< cities;
}
{ (London:England), (Moscow:Russia), (Paris:France) }