Download presentation
Presentation is loading. Please wait.
Published byJames Gibbs Modified over 9 years ago
1
1 Joe Meehean
2
List of names Set of names Map names as keys phone #’s as values Phil Bill Will Phil Bill Will Phil Bill Will Phil: 555-5699 Bill: 555-9864 Will: 555-0111 [Phil, Will, Bill]
3
Unordered collection of items without duplicates List implementation add => O(N): check for uniqueness contains => O(N) bad idea Balanced tree implementation much better (we’ll see more about this later)
4
set Key is the data type being stored Compare is a comparator Ignore Alloc (almost never needed)
5
pair insert(const T &key) inserts the key bool is true if key not already in set size_type erase(const T &key) removes the item from the set iterator find(const T &key) returns iterator to key iterator begin(), end()
6
Both store collections of objects Lists are ordered, sets are not items have a position in a list positions are meaningless in sets Duplicates list can contain duplicates sets cannot
7
Implementation lists: array or linked list sets are balanced trees or hash tables Some operations are faster in a set remove contains Use a set when… don’t have/want duplicates position does not matter need fast contains and remove
8
An unordered collection of unique keys with associated values no duplicate keys allowed Generalization of an array arrays can only be indexed by ordinal data types maps can be indexed with any comparable type What is ordinal data types? type with 1-to-1 mapping integers
9
Maps verb: a key is associated with stated value e.g., B maps to 2 symbol: B => 2 Mapping noun: key-value pair symbol: B => 2 Associative array, dictionary nouns: other words for map
10
map Key is the data type of the key Data is the data type of the value Compare is a comparison functor Ignore Alloc (almost never needed)
11
Map methods Overloads the [ ] operator Insert map[“Bill”] = 27 Access cout << map[“Bill”] << endl; Erase map.erase(“Bill”)
12
12
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.