Tutorial

Printing a vector
Printing a vector with Line-Breaks (Part I)
Printing a vector with Line-Breaks (Part II)
Printing a vector of vectors
Printing a vector of lists of pairss as XML
Printing a map
Inputting Data Structures

The 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 punctuate.

Example 1: Printing a vector

Source: <libs/format_lite/example/example1.cpp>

This example formats a 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;
}

Output:

[ London, Paris, Prague ]

Example 2: Printing a vector with Line-breaks (part I)

Source: <libs/format_lite/example/example2a.cpp>

This example formats a 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;
}

Output:

[ 
        London, 
        Paris, 
        Prague
]

Because no indentation increment was specified explicitly, the above program used the default indentation of a single tab character. If instead we wanted the 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);

This would result in the following output:

[ 
   London, 
   Paris, 
   Prague
]

Similarly, if we wanted the 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)

Source: <libs/format_lite/example/example3.cpp>

This example formats a 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;
}

Output:

[ London
  , Paris
  , Prague ]

Example 4: Printing a vector of vectors

Source: <libs/format_lite/example/example4.cpp>

This example formats a 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;
}

Output:

[ { London, Paris, Prague },
  { Rain, Snow, Fog },
  { Cat, Rabbit, Dog } ]

Example 5: Printing a vector of lists of pairs as XML

Source: <libs/format_lite/example/example5.cpp>

This example formats a 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;
}

Output:

<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

Source: <libs/format_lite/example/example6.cpp>

This example formats a 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;
}

Output:

{ (London:England), (Moscow:Russia), (Paris:France) }

Inputting Data Structures

Source: <libs/format_lite/example/example7.cpp>

In general, data structures written using operator<< can be read back using operator>> with the same punctuation sequences. These restrictions apply:

[To do: add more detailed description.]

For example,

#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;
}

Output:

{ (London:England), (Moscow:Russia), (Paris:France) }

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