03.12.2012 Views

C++ for Scientists - Technische Universität Dresden

C++ for Scientists - Technische Universität Dresden

C++ for Scientists - Technische Universität Dresden

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

4.9. STL — THE MOTHER OF ALL GENERIC LIBRARIES 121<br />

4.9 STL — The Mother of All Generic Libraries<br />

The Standard Template Library — STL — is an example of a generic C ++ library. It defines<br />

generic container classes, generic algorithms, and iterators. Online documentation is provided<br />

under www.sgi.com/tech/stl. There are also entire books written about the usage of STL so<br />

that we can keep it short here and refer to these books [?].<br />

4.9.1 Introducing Example<br />

Containers are classes whose purpose is to contain other objects. The classes vector and list are<br />

examples of STL container classes. Each of these classes is templated, and can be instantiated<br />

to contain any type of object (that is a model of the appropriate concept). For example, the<br />

following lines create a vector containing doubles and another one containing integers:<br />

std::vector vec d ;<br />

std::vector vec i ;<br />

The STL also includes a large collection of algorithms that manipulate the data stored in<br />

containers. The accumulate algorithm, <strong>for</strong> example, can be used to compute any reduction —<br />

such as sum, product, or minimum — on a list or vector in the following way:<br />

std::vector vec ; // fill the vector...<br />

std::list lst ; // fill the list...<br />

double vec sum = std::accumulate( vec.begin(), vec.end(), 0.0 ) ;<br />

double lst sum = std::accumulate( lst.begin(), lst.end(), 0.0 ) ;<br />

Notice the use of the functions begin() and end(), that denote the begin and end of the vector<br />

and the list represented by ‘Iterators’. Iterators are the central concept of the STL and we will<br />

have a closer look at it.<br />

4.9.2 Iterators<br />

Disrespectfully spoken, an iterator is a generalized pointer: one can dereference it and change the<br />

referred location. This over-simplified view is not doing justice to its importance. Iterators are a<br />

Fundamental Methodology to Decouple the Implementation of Data Structures and Algorithms.<br />

Figure 4.2 29 depicts this central role of iterators. Every data structure provides an iterator <strong>for</strong><br />

traversing it and all algorithms are implemented in terms of iterators.<br />

To program m algorithms on n data structures, one needs in classical C and Fortran programming<br />

m · n implementations.<br />

Expressing algorithms in terms of iterators decreases this to only<br />

m + n implementations!<br />

29 TODO: Flatter boxes and more containers and algos, maybe.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!