Download presentation
Presentation is loading. Please wait.
Published byDrusilla Rice Modified over 9 years ago
1
Associative Containers Sets Maps Section 4.8
2
Associative Containers
3
Generic Associative Containers A container that is organized and accessible by value Client may insert / remove any T-object in C Client may not determine position in C Bidirectional Iterators Both ++ and -- operations are supported
4
Multimodal / Unimodal Assoc. 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
5
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
6
The Pair Template Some background
7
The Pair Template A class that holds a pair of items template class Pair { public: T first; T second; Pair(); Pair(T t1, T t2); };
8
The Pair Template (contd.) Constructors template Pair ::Pair() { } template Pair ::Pair(T t1, T t2) : first(t1), second(t2) { }
9
The Pair Template (contd.) Declarations Pair intPair1(1, 2); Pair intPair2(3, 4); Pair floatPair(1.1, 2.2); Pair > anotherPair(intPair1, intPair2);
10
The Pair Template (contd.) A class that holds a pair of items with different types template class Pair { public: T1 first; T2 second; Pair(); Pair(T1 t1, T2 t2); };
11
The Pair Template (contd.) Constructors template Pair ::Pair() { } template Pair ::Pair(T1 t1, T2 t2) : first(t1), second(t2) {}
12
Sets & Maps
13
Sets Associative containers Set A sorted associative container that does not allow duplicates Stores objects Unimodal: duplicate objects not allowed MultiSet A sorted associative container that allows duplicates Stores objects Multimodal: duplicate objects OK Also known as Bags
14
The STL Set Template set() // Creates an empty set. set(const key_compare& comp) // Creates an empty set, use comp // for key comparison set(iterator f, iterator l) // Creates a set with a copy of a range set(iterator f, iterator l, const key_compare& comp) // Creates a set with a copy of a range, // using comp as the key_compare object pair insert(const value_type& x) iterator insert(iterator pos, const value_type& x) // Inserts x into the set, using pos as a hint to where it will be inserted void insert(iterator I1, iterator I2) // Inserts a range into the set
15
void erase(iterator pos) // Erases the element pointed to by pos. size_type erase(const key_type& k) // Erases the element whose key is k. void erase(iterator first, iterator last) // Erases all elements in a range. iterator find(const key_type& k) const // Finds an element whose key is k. Logarithmic complexity for insertion, remove, search
16
Example Set Clients Inventory struct StockItem { // barcode, name, amount }; void print_inventory(std::ostream&os, const set & inventory) { set ::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 customers; }
17
Maps Associative container that associates objects of type Key with objects of type Data Map Stores (key, object) pairs Unimodal on keys: duplicate keys not allowed AKA: table, associative array MultiMap Stores (key, object) pairs Multimodal: duplicate keys OK
18
The STL Map Template map() map(const key_compare& comp) map(iterator f, iterator l) map(iterator f, iterator l, const key_compare& comp) pair insert(const value_type& x) // Inserts x into the map iterator insert(iterator pos, const value_type& x) // Inserts x into the map, using pos as hint to where it will be inserted void insert(iterator, iterator) // Inserts a range into the map
19
STL Map (continued) void erase(iterator pos) // Erases the element pointed to by pos size_type erase(const key_type& k) // Erases the element whose key is k. void erase(iterator first, iterator last) // Erases all elements in a range. iterator find(const key_type& k) // Finds an element whose key is k. data_type& operator[](const key_type& k) Returns a reference to the object that is associated with a particular key. If the map does not already contain such an object, operator[] inserts the default object data_type()
20
Map Usage Example struct ltstr { bool operator()(const char* s1, const char* s2) const { return strcmp(s1, s2) < 0; } }; 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 " << months["june"] << endl; map ::iterator cur = months.find("june"); map ::iterator prev = cur; map ::iterator next = cur; ++next; --prev; cout << "Previous (in alphabetical order) is " << (*prev).first << endl; cout << "Next (in alphabetical order) is " << (*next).first << endl; }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.