Download presentation
Presentation is loading. Please wait.
Published bySamson Lucas Modified over 9 years ago
1
STL-Associative Containers
2
Associative Containers Sequence containers : "This is item 0, this is item 1, this is item 2…“ Associative containers : – Store items by key: I have the word "apple" I have the word "carrot" – No numeric index – word[0] is meaningless
3
Sets Set – Stores set of items – One copy max of each value – Items stored according to < operator Can change
4
Set Demo
5
No indexes! No index access: Always need iterators:
6
Other Set Tools find(item) returns an iterator to item – Returns end() if not found erase() takes value or iterator
7
Multiset Same as Set, but duplicates allowed – Also in Set library
8
Grouping Things Complex groupings deserve struct/class – Class if has behaviors – Struct – simple data Do we really want lots of structs like ?
9
Pair Pair template in library Looks like:
10
Pair Use Only has first/second Can "construct" with initializer list
11
Pair Use Can make a vector of pairs: But associations not explicitly stored – No order – No easy way to look up things
12
Map Stores a sorted list of key/value pairs Key A B C D E Value Apple Banana Carrot Dingo Elf
13
Map Key used as index into list – Assigning creates record Key A B C D E Value Apple Banana Carrot Dingo Elf
14
Map Gotcha Accessing a key adds it to the Map with default value
15
Ways to insert: Insertions – Map stores pairs, multiple ways to add:
16
Ways to insert: Each key can have only one value:
17
Iteration: Use iterators just like vector – Access parts with first/second Map stored ordered by keys
18
Find/Contains Can find(keyType): – No find(valueType)
19
Erase Erase takes key or iterator: – Not a value
20
MultiMap To store list as value, could use map of vector: Can’t use [] with multimap
21
Multimap Equal_range returns pair of iterators – first : First location where we found a match – second : One past last location of match – first == second : not there
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.