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.

204 CHAPTER 7. EFFECTIVE PROGRAMMING: THE POLYMORPHIC WAY<br />

copy(InputIterator first, InputIterator last, OutputIterator result)<br />

{<br />

while (first != last)<br />

∗result++ = ∗first++;<br />

return result;<br />

}<br />

With this code the same functionality of the memcpy() from the C library is achieved.<br />

All kinds of data structure which offer an begin() and end() iterator can be used.<br />

• Container: An abstraction to all kinds of data structures which can store other data<br />

types.<br />

• Iterator: This is the glue between the containers and the algorithms. First, it separates<br />

the usage of data structures and algorithms. Second, it provides a concept hierarchy <strong>for</strong><br />

all kinds of traversal within data structures.<br />

This type of genericity is called parametric polymorphism (see Section 7.5.2). Section 4.9<br />

introduced the Standard Template Library (STL). The STL solves many standard data structure<br />

and algorithmic problems. The STL is (or should be) the first choice in all code development<br />

steps.<br />

• Algorithm/Data-Structure Interoperability: First, each algorithm is written in a datastructure<br />

neutral way, allowing a single template function to operate on many different<br />

classes of containers. The concept of an iterator is the key ingredient in this decoupling of<br />

algorithms and data-structures. The impact of this technique is a reduction of the STL’s<br />

code size from O(M*N) to O(M+N), where M is the number of algorithms and N is the<br />

number of containers. Considering a situation of 20 algorithms and 5 data-structures,<br />

this makes the difference between writing 100 functions versus only 25 functions! And the<br />

differences grows faster as the number of algorithms and data-structures increase.<br />

• Extension through Function Objects: The second way that the STL is generic is that its<br />

algorithms and containers are extensible. The user can adapt and customize the STL<br />

through the use of function objects. This flexibility is what makes STL such a great<br />

tool <strong>for</strong> solving real-world problems. Each programming problem brings its own set of<br />

entities and interactions that must be modeled. Function objects provide a mechanism<br />

<strong>for</strong> extending the STL to handle the specifics of each problem domain.<br />

• Element Type Parametrization: The third way that STL is generic is that its containers<br />

are parametrized on the element type.<br />

Most people think, that element type parametrization is the feature that makes the successful.<br />

This is perhaps the least interesting way in which STL is generic. The interoperability with<br />

iterators and the extensibility by function objects are more important parts of the STL. But<br />

the essence is the programming with concepts. The programmer can write the data structures<br />

and algorithms, or in other words the concept of these, as it should be. Next to these facts, the<br />

STL has proven that with the generic programming paradigm, high per<strong>for</strong>mance computing<br />

can be accomplished as well on several different computer architectures.<br />

The advantages of this paradigm are:<br />

• Programming with concepts<br />

• Great number of available libraries

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

Saved successfully!

Ooh no, something went wrong!