Sequence Containers Associative Containers Sets and Maps Section

  • Slides: 12
Download presentation
Sequence Containers, Associative Containers Sets and Maps • Section 4. 8

Sequence Containers, Associative Containers Sets and Maps • Section 4. 8

Generic Positional Container • A generic container C<T> that is – Organized and accessed

Generic Positional Container • A generic container C<T> that is – Organized and accessed by position • The order of elements in container is determined by the order in which they are inserted into container • “p. Container”, for short – Also known as “sequence containers” • Examples: Vector, List, Stack, Queue, and Deque 2

Generic Associative Containers • A generic container C that is – Organized and accessible

Generic Associative Containers • A generic container C that is – Organized and accessible by value – Positional details left to implementation – The order of elements in container is not only determined by the order in which they are inserted into container • “a. Container” for short • Examples: map, set, and hash tables 3

Multimodal vs. Unimodal Associative Containers • Multimodal associative container – Duplicate elements are allowed

Multimodal vs. Unimodal Associative Containers • Multimodal associative container – Duplicate elements are allowed – Insert operations always increase size by 1 • Unimodal associative container – Duplicate elements not allowed – Insert operations have dual personality • If t is not in C, then Insert(t) • If t is in C, then overwrite the existing t 4

Generic Sorted Associative Containers • A generic associative container • Traverse elements in sorted

Generic Sorted Associative Containers • A generic associative container • Traverse elements in sorted order for (C: : Iterator I = c. Begin(); I != c. End(); ++I) { cout << *I; } • May be either unimodal or multimodal 5

The Pair Template • A class that holds a pair of items (may be

The Pair Template • A class that holds a pair of items (may be of different types) template <typename T 1, typename T 2> class Pair { public: T 1 first; T 2 second; Pair() {}; Pair(const T 1& t 1, const T 2& t 2): first{t 1}, second{t 2} {}; }; • Examples Pair<int, int> pair 1; Pair<string, int> pair 2; • C++/STL #include <utility> pair<int, int> pair 1 pair<string, int> pair 2 6

C++ STL Sets & Maps • See examples/r 6 for details

C++ STL Sets & Maps • See examples/r 6 for details

Sets • Sorted associative containers • Set – A sorted associative container that does

Sets • Sorted associative containers • Set – A sorted associative container that does not allow duplicates – Stores objects – Unimodal: duplicate objects not allowed • Multi. Set – – A sorted associative container that allows duplicates Stores objects Multimodal: duplicate objects OK Also known as bags 8

Example Set Clients • Inventory struct Stock. Item { // barcode, name, amount };

Example Set Clients • Inventory struct Stock. Item { // barcode, name, amount }; void print_inventory(std: : ostream&os, const set<Stock. Item>& inventory) { set<Stock. Item>: : iterator I; for (I = inventory. begin(); I != inventory. end(); ++I) { os << *I; } } • Customer accounts class Customer { // ssn, account_number, last_name, first_name… }; int main() { set<Customer> customers; } 9

Maps • Associative container that associates objects of type Key with objects of type

Maps • Associative container that associates objects of type Key with objects of type Data – Sorted according to keys • Map – Stores (key, object) pairs – Unimodal: duplicate keys not allowed – AKA: table, associative array • Multi. Map – Stores (key, object) pairs – Multimodal: duplicate keys OK 10

Map Usage Example //typedef map<string, int> MAP; //typedef MAP: : iterator ITR; using MAP

Map Usage Example //typedef map<string, int> MAP; //typedef MAP: : iterator ITR; using MAP = map<string, int >; int main() { MAP months; months["january"] = 31; months["february"] = 28; months["march"] = 31; months["april"] = 30; months["may"] = 31; months["june"] = 30; months["july"] = 31; months["august"] = 31; months["september"] = 30; months["october"] = 31; months["november"] = 30; months["december"] = 31; cout << "june: " << months["june"] << endl; auto cur = months. find("june"); cout << (*cur). first << “: “ << (*cur). second << endl; cout << cur->first << “: “ << cur->second << endl; } 11

Reading assignment • Hashing (Chapter 5) 12

Reading assignment • Hashing (Chapter 5) 12