legacy:manuals:en:develop:appendix_a_stl
no way to compare when less than two revisions
Differences
This shows you the differences between two versions of the page.
— | legacy:manuals:en:develop:appendix_a_stl [2023/03/13 01:46] (current) – created - external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | |||
+ | |||
+ | |||
+ | ====== Appendix A: The C++ Standard Template Library ====== | ||
+ | |||
+ | The Standard Template Library (STL) is a widely-used C++ library from Silicon Graphics (// www.sgi.com //). This Appendix gives a brief overview of key parts that are used throughout the Greenstone code. For a fuller description, | ||
+ | |||
+ | As the word “template” suggests, STL is not just a plug-and-use object library. Coupled with the template mechanism in C++, it provides a forum for programmers to concisely develop their own objects that tap into the algorithmic capabilities embedded within STL. This adds an extra layer of complexity, but it's worth it. | ||
+ | |||
+ | To help understand the Greenstone code excerpts given in this manual, we give a few tutorial level examples that use STL. | ||
+ | |||
+ | ===== Lists ===== | ||
+ | |||
+ | < | ||
+ | <code 1> | ||
+ | #include < | ||
+ | |||
+ | #define nil 0 | ||
+ | |||
+ | struct intlist { | ||
+ | int val; | ||
+ | | ||
+ | }; | ||
+ | |||
+ | int total_int_list(intlist* head) | ||
+ | { | ||
+ | int total = 0; | ||
+ | | ||
+ | while (curr!=nil) | ||
+ | { | ||
+ | total += curr-> | ||
+ | curr = curr-> | ||
+ | } | ||
+ | |||
+ | | ||
+ | } | ||
+ | |||
+ | void main() | ||
+ | { | ||
+ | | ||
+ | | ||
+ | | ||
+ | |||
+ | int total = total_int_list(& | ||
+ | cout << " List items total to: " << total << endl; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | |||
+ | |||
+ | First we study two programs that implement an integer list. One uses basic C++ types (the “old fashioned” way), the other uses STL. Figure <imgref figure_programming_a_list_of_integers> | ||
+ | |||
+ | To demonstrate use of the data structure, the main program (lines 23—32) sets up an integer list with elements [5, 4]. It then calls the function // | ||
+ | |||
+ | The main work is accomplished by lines 12—18. First some initialisation: | ||
+ | |||
+ | < | ||
+ | < | ||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | int total_int_list(list< | ||
+ | { | ||
+ | int total = 0; | ||
+ | | ||
+ | while (curr!=head-> | ||
+ | { | ||
+ | total += *curr; | ||
+ | | ||
+ | } | ||
+ | |||
+ | | ||
+ | } | ||
+ | |||
+ | void main() | ||
+ | { | ||
+ | | ||
+ | | ||
+ | | ||
+ | |||
+ | int total = total_int_list(& | ||
+ | cout << " List items total to: " << total << endl; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | |||
+ | |||
+ | Figure <imgref figure_programming_a_list_of_integers_using_stl> | ||
+ | |||
+ | The main work is done by lines 6—12. There are still two initialisations and a while loop, but other than that the new syntax has little in common with the old. Central to this new way of processing is a variable of type // | ||
+ | |||
+ | The STL implementation of this program is slightly smaller (25 lines verses 31) than the conventional code. The gains are more noticeable in larger projects, because the STL //list// object is more powerful than the example here illustrates. It is, for instance, a doubly linked list that supports several forms of insertion and deletion—something that would require additional programming effort to add to the basic integer list version. | ||
+ | |||
+ | Note that the parameter to // | ||
+ | |||
+ | ===== Maps ===== | ||
+ | |||
+ | When implementing a digital library system, it is useful to be able to store elements in an array indexed by text strings rather than by numeric indexes. In Greenstone, for example, this greatly simplifies storing the macro files once they have been read; and the various configuration files. A data type that supports such access is called an // | ||
+ | |||
+ | < | ||
+ | <code 1> | ||
+ | #include < | ||
+ | #include <map> | ||
+ | |||
+ | int total_int_table(map< | ||
+ | { | ||
+ | int total = 0; | ||
+ | | ||
+ | while (curr!=table.end()) | ||
+ | { | ||
+ | total += (curr-> | ||
+ | | ||
+ | } | ||
+ | |||
+ | | ||
+ | } | ||
+ | |||
+ | int main() | ||
+ | { | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | |||
+ | int total = total_int_table(table); | ||
+ | cout << " Age total: " << total << endl; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | |||
+ | |||
+ | In STL, associative arrays are accomplished using the //map// object. Figure <imgref figure_using_associative_arrays_in_stl> | ||
+ | |||
+ | Like //list//, //map// is a container class. However, when declaring a variable of this type we must specify two((Technically there are four types, but the last two are optional. Since we are only giving a basic introduction to this STL class, details about these last two types are omitted.)) things: the index type, and the element type. As can be seen on line 19, we obtain an associative array that stores integers using // | ||
+ | |||
+ | There are several ways to store elements in the associative array. In the example on lines 20—22 the overloaded array subscript [ ] is used to initialise the table with the ages of the three people. The similarity of // | ||
+ | |||
+ | Two other STL types widely used in the Greenstone code are //vector// and //set//. The former facilitates dynamic arrays, and the latter supports mathematical set operations such as union, intersection and difference. | ||
legacy/manuals/en/develop/appendix_a_stl.txt · Last modified: 2023/03/13 01:46 by 127.0.0.1