Download presentation
Presentation is loading. Please wait.
Published byAlfred Waters Modified over 9 years ago
1
CSE 332: C++ Associative Containers II Associative Containers’ Associated Types Associative containers declare additional types –A key_type gives the type of keys the container holds –A value_type gives the type of values the container holds –A mapped_type gives type associated with key (maps only) Examples for sets (key and value types are the same) –Associated type set ::key_type is int, as is set ::value_type Examples for maps (note key becomes const in value) –Associated type map ::key_type is int –Type map ::mapped_type is string –Associated type map ::value_type is pair
2
CSE 332: C++ Associative Containers II Iteration Through Associative Containers Iteration is bi-directional –Can increment (e.g., ++iter1 ) or decrement (e.g., --iter1 ) an iterator Dereferencing gives read-only access to keys –E.g., *iter1 = “D”; or iter2->first = “D”; are not allowed But, can gain read-write access to mapped value –E.g., iter2->second = 7; is allowed set multiset map multimap 2B 3A 5C 2C 7D 2B 3A 2C 7D B A C C D B A C D iter1 iter2
3
CSE 332: C++ Associative Containers II Adding Elements to a Map or Set Insert returns a pair (may also rebalance the tree) –First part is an iterator to element, second is bool ( true on success) –Multimap or multiset insert just returns an iterator to inserted element For maps, insert takes a pair (several possible ways) –E.g., m.insert({“C”, 5}); if compiler supports list initialization –E.g., m.insert(make_pair(“C”, 5)); –E.g., m.insert(pair (“C”, 5)); –E.g., m.insert(map ::value_type(“C”, 5)); set (before) set (after) map (before) 2B 3A 5C 2D 7E 2B 3A 2D 7E B C D E E map (after) B D
4
CSE 332: C++ Associative Containers II Finding Elements in a Multimap or Multiset Find returns an iterator to first matching element –Forward iteration from first matching element (if one was found) gives all other equal elements (find returns past the end iterator if no match) Can obtain iterators bounding range of equal elements –E.g., s.lower_bound(“B”); gives first element not less than “B” –E.g., s.upper_bound(“B”); gives first element greater than “B” –E.g., s.equal_range(“B”); returns a pair of iterators bounding the range of elements with key “B” (or insertion point if match not found) 2B 3A 5B 2C 7C B A B C C iter1 iter2 iter1 iter2
5
CSE 332: C++ Associative Containers II Unordered Containers (UCs) UCs use == to compare elements instead of < to order them –Types in unordered containers must be equality comparable –When you write your own structs, overload == as well as < UCs store elements in indexed buckets instead of in a tree –Useful for types that don’t have an obvious ordering relation over their values UCs use hash functions to put and find elements in buckets –May improve performance in some cases (if performance profiling suggests so) –Declare UCs with pluggable hash functions via callable objects, decltype, etc. A unordered_setunordered_multiset unordered_mapunordered_multimap 0BC ABC C A7B2C 0A7B2C 3C
6
CSE 332: C++ Associative Containers II Concluding Remarks Choose carefully which operators you use –E.g.,, m.insert(map ::value_type(“C”, 5)); avoids construct/destruct/assign of a temporary (vs. m[“C”] = 5); ) Also realize that overloaded operator[] has different interpretations in different containers –In a vector or other random access sequence container it’s a constant time indexing operation to a particular location –In a map or other associative container it’s a logarithmic time operation (“find” or “insert” versus “read” or “write”) Unordered containers give another mix of operations –E.g., via hashing, for which callable objects can be used
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.