19. STL Container Programmieren / Algorithmen und Datenstrukturen 2

Prof. Dr. Bernhard Humm FB Informatik, Hochschule Darmstadt Wintersemester 2012 / 2013

1

Prof. Dr. Bernhard Humm, FB Informatik, Hochschule Darmstadt. www.fbi.h-da.de/~b.humm. 06.01.2014

Agenda • Kontrollfragen

• STL Container: Übersicht • Übung • Assoziative Container: map

• Übung

2

Prof. Dr. Bernhard Humm, FB Informatik, Hochschule Darmstadt. www.fbi.h-da.de/~b.humm. 06.01.2014

Kontrollfragen • Was bedeutet „Design by Contract“?

• Wer schließt einen Vertrag mit wem ab? Wo wird der Vertrag beschrieben? • Was sind Vorbedingungen? Wer muss sie einhalten?

• Was sind Nachbedingungen? Wer muss sie einhalten? • Wie sollte man mit Vertragsverletzungen umgehen (defensives Programmieren)?

3

Prof. Dr. Bernhard Humm, FB Informatik, Hochschule Darmstadt. www.fbi.h-da.de/~b.humm. 06.01.2014

Agenda • Kontrollfragen

• STL Container: Übersicht • Übung • Assoziative Container: map

• Übung

4

Prof. Dr. Bernhard Humm, FB Informatik, Hochschule Darmstadt. www.fbi.h-da.de/~b.humm. 06.01.2014

C++ Standard Template Library (STL) • Eine der beliebtesten C++- Klassenbibliotheken

• Ergebnis jahrelanger Forschungs- und Entwicklungsarbeit bei Hewlett-Packard • Verlässliche, erprobte Komponenten

• Konsistente, gut verständliche Architektur • 1994 standardisiert (ISO/IEC 14882) • http://www.cplusplus.com/reference/stl/

5

Prof. Dr. Bernhard Humm, FB Informatik, Hochschule Darmstadt. www.fbi.h-da.de/~b.humm. 06.01.2014

Behälter (Container) • A container is a holder object that stores a collection other objects (its elements). They are implemented as class templates, which allows a great flexibility in the types supported as elements. • The container manages the storage space for its elements and provides member functions to access them, either directly or through iterators. • Containers replicate structures very commonly used in programming: dynamic arrays (vector), queues (queue), stacks (stack), heaps (priority_queue), linked lists (list), trees (set), associative arrays (map)...

Quelle: http://www.cplusplus.com/reference/stl/ 6

Prof. Dr. Bernhard Humm, FB Informatik, Hochschule Darmstadt. www.fbi.h-da.de/~b.humm. 06.01.2014

Übersicht Container-Klassen STL

Quelle: http://www.cplusplus.com/reference/stl/ 7

Prof. Dr. Bernhard Humm, FB Informatik, Hochschule Darmstadt. www.fbi.h-da.de/~b.humm. 06.01.2014

8

Prof. Dr. Bernhard Humm, FB Informatik, Hochschule Darmstadt. www.fbi.h-da.de/~b.humm. 06.01.2014

9

Prof. Dr. Bernhard Humm, FB Informatik, Hochschule Darmstadt. www.fbi.h-da.de/~b.humm. 06.01.2014

Elemente eines Containers iterieren (1/2) • Für Container mit Direktzugriff mittels operator[ ] (z.B. Vector): for (unsigned int i = 0; i < v.size(); ++i) { sum += v[i]; }

• Mittels Iterator (funktioniert für alle STL Container): for (vector::iterator it = v.begin(); it < v.end(); ++it) { sum += *it; }

• Einfacher in C++11 mit auto (Typ Inferenz) for (auto it = v.begin(); it < v.end(); ++it) { sum += *it; Compiler setzt automatisch korrekten Datentyp } des Iterators ein 10

Prof. Dr. Bernhard Humm, FB Informatik, Hochschule Darmstadt. www.fbi.h-da.de/~b.humm. 06.01.2014

Elemente eines Containers iterieren (2/2) • Noch einfacher mittels erweiterter for Schleife in C++11: for (float x : v) { sum += x; }

Schleifenvariable wird jeweils auf nächstes Element gesetzt

• Bzw. mit auto (Typ Inferenz): for (auto x : v) { sum += x; }

• Bzw. mittels Call-By-Reference: for (auto &x : v) { sum += x; } 11

Prof. Dr. Bernhard Humm, FB Informatik, Hochschule Darmstadt. www.fbi.h-da.de/~b.humm. 06.01.2014

C++11 in NetBeans aktivieren • Project  Properties  C++ Compiler

12

Prof. Dr. Bernhard Humm, FB Informatik, Hochschule Darmstadt. www.fbi.h-da.de/~b.humm. 06.01.2014

Weiterführende Literatur • B. Stroustrup: „The C++ Programming Language“ 3rd Edition http://www.ib.cnea.gov.ar/~oop/biblio/Bjarne_Stroustrup__The_C++_Programming_Language_3rd_Ed.pdf - 3.7 Containers - 3.8.1 Use of Iterators - 16. Library Organization and Containers

13

Prof. Dr. Bernhard Humm, FB Informatik, Hochschule Darmstadt. www.fbi.h-da.de/~b.humm. 06.01.2014

Agenda • Kontrollfragen

• STL Container: Übersicht • Übung • Assoziative Container: map

• Übung

14

Prof. Dr. Bernhard Humm, FB Informatik, Hochschule Darmstadt. www.fbi.h-da.de/~b.humm. 06.01.2014

Übung Angelehnt an: B. Stroustrup: „The C++ Programming Language“, 16.5

15

1.

Create a vector containing the letters of the alphabet in order. Print the elements of that vector in order and in reverse order

2.

Create a vector and initialize it with some names of fruits. Sort the list.

3.

Using the vector from exercise 2, select the names of all fruits with the initial letter 'a'.

4.

Using the vector from exercise 2, write a loop to delete all fruits with the initial letter 'a'.

5.

Using the vector from exercise 2, write a loop to delete all citrus fruits.

6.

Using the vector from exercise 2, write a loop to delete all fruits that you don't like.

Prof. Dr. Bernhard Humm, FB Informatik, Hochschule Darmstadt. www.fbi.h-da.de/~b.humm. 06.01.2014

Agenda • Kontrollfragen

• STL Container: Übersicht • Übung • Assoziative Container: map

• Übung

16

Prof. Dr. Bernhard Humm, FB Informatik, Hochschule Darmstadt. www.fbi.h-da.de/~b.humm. 06.01.2014

Assoziative Container: map • Sequence of (key,value) pairs that provides for fast retrieval based on the key • Each key in a map is unique • Requires that a less-than operation (second; Prüfung auf Existenz } durch Vergleich mit end() auto result = phoneBook.find("BlaBla"); if (result != phoneBook.end()) { string number = result->second; } 20

Prof. Dr. Bernhard Humm, FB Informatik, Hochschule Darmstadt. www.fbi.h-da.de/~b.humm. 06.01.2014

Mmber Functions

21

Prof. Dr. Bernhard Humm, FB Informatik, Hochschule Darmstadt. www.fbi.h-da.de/~b.humm. 06.01.2014

Agenda • Kontrollfragen

• STL Container: Übersicht • Übung • Assoziative Container: map

• Übung

22

Prof. Dr. Bernhard Humm, FB Informatik, Hochschule Darmstadt. www.fbi.h-da.de/~b.humm. 06.01.2014

Übung: Histogramm • Zählen Sie die Häufigkeit von Worten in einer Liste / Vector und geben Sie diese als map zurück , z.B.: - Input: {Apfel, Birne, Apfel, Banane} - Output: Apfel  2, Birne  1, Banane  1

• Gestalten Sie die Außensicht sorgfältig (Klasse? / Funktion? / Eingabeparameter? / Rückgabe?)

23

Prof. Dr. Bernhard Humm, FB Informatik, Hochschule Darmstadt. www.fbi.h-da.de/~b.humm. 06.01.2014