Introduction to SourcePro : Introduction to SourcePro® : SourcePro Core : An Example Using the Essential Tools Module and the C++ Standard Library
An Example Using the Essential Tools Module and the C++ Standard Library
The C++ Standard Library is perhaps the ultimate in general purpose functionality, but its range of low-level features can be daunting. The Essential Tools Module builds on the standard library to provide richer interfaces and easier semantics. At the same time, the design preserves the ability to exchange objects between the libraries so that they work well together.
In the following example, the RWTValDlist class template from the Essential Tools Module is used to construct a doubly-linked list of integers. Once the list is created, the Essential Tools Module functionality is used to add elements, sort, and persist the list. At the same time, algorithms from the C++ Standard Library are used to manipulate the list and output the results. The begin() and end() member functions of RWTValDlist return bidirectional iterators of the C++ Standard Library std::list container that it builds on. If desired, you could also use the std() member function to access the list container directly and perform any C++ Standard Library operations with it.
Example 1 – Using the Essential Tools Module and the C++ Standard Library
// Permutations example:
// This example shows the Essential Tools Module and
// the C++ Standard Library working together.
 
// From the Essential Tools Module
#include <rw/bstream.h>
#include <rw/tvdlist.h>
 
// From the C++ Standard Library
#include <algorithm>
#include <fstream>
#include <iostream>
using std::cout;
using std::endl;
using std::for_each;
using std::ifstream;
using std::next_permutation;
using std::ofstream;
using std::reverse;
 
void outputInt(int i)
{
cout << i << " ";
}
 
int main()
{
// The first block in main creates, reverses,
// and persists the list.
{
RWTValDlist<int> dlist;
 
// Insert numbers into dlist
for (int i = 1; i < 5; ++i) {
dlist.insert(i);
}
 
// Reverse the dlist
reverse(dlist.begin(), dlist.end());
 
// Print out the result
cout << "\nThe dlist reversed:\n";
for_each(dlist.begin(), dlist.end(), outputInt);
cout << "\n" << endl;
 
// Persist the container
cout << "Writing out the container...\n\n";
ofstream ofstrm("dlist.dat");
RWbostream obstrm(ofstrm);
obstrm << dlist;
}
 
// The second block in main restores the list, sorts it again,
// prints the sorted list, and finally prints all permutations
// of the list items.
{
RWTValDlist<int> dlist;
 
// Restore the container from our file
cout << "Reading in the container...\n\n";
ifstream ifstrm("dlist.dat");
RWbistream ibstrm(ifstrm);
ibstrm >> dlist;
 
// Print out sorted dlist
cout << "The dlist sorted:\n";
dlist.sort();
for_each(dlist.begin(), dlist.end(), outputInt);
cout << "\n" << endl;
 
// Print out permutations
cout << "Permutations of the dlist:\n";
int permutations = 1;
 
while (next_permutation(dlist.begin(), dlist.end())) {
for_each(dlist.begin(), dlist.end(), outputInt);
cout << "\n";
++permutations;
}
cout << "\n";
cout << "Total Permutations: " << permutations
<< "\n" << endl;
}
return 0;
}
 
Program output:
 
The dlist reversed:
4 3 2 1
 
Writing out the container...
 
Reading in the container...
 
The dlist sorted:
1 2 3 4
 
Permutations of the dlist:
1 2 4 3
1 3 2 4
1 3 4 2
1 4 2 3
1 4 3 2
2 1 3 4
2 1 4 3
2 3 1 4
2 3 4 1
2 4 1 3
2 4 3 1
3 1 2 4
3 1 4 2
3 2 1 4
3 2 4 1
3 4 1 2
3 4 2 1
4 1 2 3
4 1 3 2
4 2 1 3
4 2 3 1
4 3 1 2
4 3 2 1
 
Total Permutations: 24