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.
This example has been tested internally at Rogue Wave, but not in all possible configurations. Depending on your environment and the options chosen when you built your SourcePro libraries, minor syntactical changes may be needed for a successful build.
// Permutations example: // This example shows the Essential Tools Module and // the C++ Standard Library working together. // From the C++ Standard Library #include <algorithm> #include <fstream> #include <iostream> // From the Essential Tools Module #include <rw/tvdlist.h> #include <rw/bstream.h> void outputInt(int i) { std::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 std::reverse(dlist.begin(), dlist.end()); // Print out the result std::cout << "\nThe dlist reversed:\n"; std::for_each(dlist.begin(), dlist.end(), outputInt); std::cout << "\n" << std::endl; // Persist the container std::cout << "Writing out the container...\n\n"; std::ofstream ofstrm("dlist.dat"); RWbostream obstrm(ofstrm); obstrm << dlist; } // The second block in main restores the list, resorts it, // prints the sorted list, and finally prints all permutations // of the list items. { RWTValDlist<int> dlist; // Restore the container from our file std::cout << "Reading in the container...\n\n"; std::ifstream ifstrm("dlist.dat"); RWbistream ibstrm(ifstrm); ibstrm >> dlist; // Print out sorted dlist std::cout << "The dlist sorted:\n"; dlist.sort(); std::for_each(dlist.begin(), dlist.end(), outputInt); std::cout << "\n" << std::endl; // Print out permutations std::cout << "Permutations of the dlist:\n"; int permutations = 0; while(++permutations, std::next_permutation(dlist.begin(), dlist.end())) { std::for_each(dlist.begin(), dlist.end(), outputInt); std::cout << "\n"; } std::cout << "\n"; std::cout << "Total Permutations: " << permutations << "\n" << std::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 |
Copyright © Rogue Wave Software, Inc. All Rights Reserved.
The Rogue Wave name and logo, and SourcePro, are registered trademarks of Rogue Wave Software. All other trademarks are the property of their respective owners.
Provide feedback to Rogue Wave about its documentation.