STL Container - Hochschule Darmstadt
Transcrição
STL Container - Hochschule Darmstadt
18. 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. Wintersemester 2012 / 2013 Agenda • STL Container: Iteratoren • Übung • Assoziative Container: map • Übung 2 Prof. Dr. Bernhard Humm, FB Informatik, Hochschule Darmstadt. www.fbi.h-da.de/~b.humm. Wintersemester 2012 / 2013 C++ Standard Template Library (STL) • Eine der beliebtesten C++-Basisbibliotheken • Ergebnis jahrelanger Forschungsarbeit bei Hewlett-Packard • Umfangreiche Klassenbibliothek • Konsistente, elegante und daher gut verständliche Architektur • Verlässliche, erprobte Komponenten • 1994 standardisiert (ISO/IEC 14882 • http://www.cplusplus.com/reference/stl/ 3 Prof. Dr. Bernhard Humm, FB Informatik, Hochschule Darmstadt. www.fbi.h-da.de/~b.humm. Wintersemester 2012 / 2013 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/ 4 Prof. Dr. Bernhard Humm, FB Informatik, Hochschule Darmstadt. www.fbi.h-da.de/~b.humm. Wintersemester 2012 / 2013 Übersicht Container-Klassen STL Quelle: http://www.cplusplus.com/reference/stl/ 5 Prof. Dr. Bernhard Humm, FB Informatik, Hochschule Darmstadt. www.fbi.h-da.de/~b.humm. Wintersemester 2012 / 2013 Iteratoren: Elemente eines Containers durchgehen Quelle: http://www.cplusplus.com/reference/stl/vector/begin/ 6 Prof. Dr. Bernhard Humm, FB Informatik, Hochschule Darmstadt. www.fbi.h-da.de/~b.humm. Wintersemester 2012 / 2013 Vereinfachungen in C++11 • Type Inference: auto Compiler ersetzt automatisch korrekten Datentyp des Iterators for (auto it = myvector.begin() ; it != myvector.end(); ++it) { cout << ' ' << *it; } • Noch einfacher: for each Schleife: Schleifenvariable wird jeweils auf nächstes Element gesetzt for (int &n : myvector) { cout << ' ' << n; } • Unterstützung des neuen Standards abhängig vom Compiler (g++4 unterstützt Type Inference, aber nicht for each Schleife) 7 Prof. Dr. Bernhard Humm, FB Informatik, Hochschule Darmstadt. www.fbi.h-da.de/~b.humm. Wintersemester 2012 / 2013 C++11 in NetBeans aktivieren • Project Properties C++ Compiler Additional Options std=c++0x 8 Prof. Dr. Bernhard Humm, FB Informatik, Hochschule Darmstadt. www.fbi.h-da.de/~b.humm. Wintersemester 2012 / 2013 STL Container und Iteratoren in C++ • 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 • Cplusplus.com: STL Containers - http://www.cplusplus.com/reference/stl/ 9 Prof. Dr. Bernhard Humm, FB Informatik, Hochschule Darmstadt. www.fbi.h-da.de/~b.humm. Wintersemester 2012 / 2013 Agenda • STL Container: Iteratoren • Übung • Assoziative Container: map • Übung 10 Prof. Dr. Bernhard Humm, FB Informatik, Hochschule Darmstadt. www.fbi.h-da.de/~b.humm. Wintersemester 2012 / 2013 Übung Angelehnt an: B. Stroustrup: „The C++ Programming Language“, 16.5 11 1. Create a vector<char> containing the letters of the alphabet in order. Print the elements of that vector in order and in reverse order 2. Create a vector<string> 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. Wintersemester 2012 / 2013 Agenda • STL Container: Iteratoren • Übung • Assoziative Container: map • Übung 12 Prof. Dr. Bernhard Humm, FB Informatik, Hochschule Darmstadt. www.fbi.h-da.de/~b.humm. Wintersemester 2012 / 2013 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 (<) exist for its key types • Keeps its elements sorted so that iteration occurs in order Quelle: B. Stroustrup: „The C++ Programming Language“, 17.4 Siehe http://www.cplusplus.com/reference/map/ 13 Prof. Dr. Bernhard Humm, FB Informatik, Hochschule Darmstadt. www.fbi.h-da.de/~b.humm. Wintersemester 2012 / 2013 Beispiel: Telefonbuch Operator[ ] #include <map> #include <string> map<string, string> phoneBook; phoneBook["Meier"] = "123"; phoneBook["Müller"] = "456"; phoneBook["Huber"] = "789"; Einfügen von Key-ValuePaaren in die Map Auslesen des Values für einen existierenden Key string number = phoneBook["Müller"]; // 456 • Achtung: bei nicht existierendem Element, z.B. number = phoneBook["BlaBla"]; wird ein neues Element eingefügt (!) 14 Prof. Dr. Bernhard Humm, FB Informatik, Hochschule Darmstadt. www.fbi.h-da.de/~b.humm. Wintersemester 2012 / 2013 Beispiel: Telefonbuch: insert, find map<string, string> phoneBook; Einfügen von Key-ValuePaaren in die Map phoneBook.insert( pair<string, string>("Meier", "123")); phoneBook.insert( pair<string, string>("Müller", "456")); phoneBook.insert( pair<string, string>("Huber", "789")); find liefert einen Iterator auto result = phoneBook.find("Meier"); auf ein pair if (result != phoneBook.end()) { string number = result->second; Prüfung auf Existenz } durch Vergleich mit end() auto result = phoneBook.find("BlaBla"); if (result != phoneBook.end()) { string number = result->second; } 15 Prof. Dr. Bernhard Humm, FB Informatik, Hochschule Darmstadt. www.fbi.h-da.de/~b.humm. Wintersemester 2012 / 2013 Mmber Functions Quelle: http://www.cplusplus.com/reference/stl/map/ 16 Prof. Dr. Bernhard Humm, FB Informatik, Hochschule Darmstadt. www.fbi.h-da.de/~b.humm. Wintersemester 2012 / 2013 Literatur zum C++ Container map • 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.4 Map - 17.4 Associative Containers • Cplusplus.com: map - http://www.cplusplus.com/reference/stl/map/ 17 Prof. Dr. Bernhard Humm, FB Informatik, Hochschule Darmstadt. www.fbi.h-da.de/~b.humm. Wintersemester 2012 / 2013 Agenda • STL Container: Iteratoren • Übung • Assoziative Container: map • Übung 18 Prof. Dr. Bernhard Humm, FB Informatik, Hochschule Darmstadt. www.fbi.h-da.de/~b.humm. Wintersemester 2012 / 2013 Übung: Histogramm • Entwickeln Sie Software, welche die Häufigkeit Vorkommens jedes Wortes in einem Text zählt und als map<string, int> zurück gibt • Gestalten Sie die Außensicht sorgfältig (Klasse? / Methode? / Eingabeparameter? / Rückgabe?) • Testen Sie die Software mittels CppUnit 19 Prof. Dr. Bernhard Humm, FB Informatik, Hochschule Darmstadt. www.fbi.h-da.de/~b.humm. Wintersemester 2012 / 2013