Presentation is loading. Please wait.

Presentation is loading. Please wait.

STL multimap Container. STL multimaps multimaps are associative containers –Link a key to a value –AKA: Hashtables, Associative Arrays –A multimap allows.

Similar presentations


Presentation on theme: "STL multimap Container. STL multimaps multimaps are associative containers –Link a key to a value –AKA: Hashtables, Associative Arrays –A multimap allows."— Presentation transcript:

1 STL multimap Container

2 STL multimaps multimaps are associative containers –Link a key to a value –AKA: Hashtables, Associative Arrays –A multimap allows a single key to be associated with multiple values i.e. a city and all clients that live in that city Key and Value can be of arbitrary types Key type is consistent, Value type is consistent within table.

3 STL multimaps Still use #include multimaps are templated Template fields (multimap ) require key type, value type, compare function, allocator function –Allocator is almost never needed –Compare, if not used defaults to, less (<) function –Should provide an implementation of less if it is not naturally defined for the types (ie for any objects you build yourself) or put in your own compare function multimap m; // simple constructor, for holding // keys of type char, values of type int // less than is already defined for char

4 STL multimaps: Constructors Same basic constructors as map: Standard: multimap mapName(const Comp &cmpfn = Comp(), const Allocator &a = Allocator()); Copy: multimap mapName(const map &anotherMap); Copy Range: template multimap(InIter start, InIter end, const Comp &cmpfn = Comp(), const Allocator &a = Allocator());

5 STL Maps: Data Storage Tree structure –logarithmic time inserts, finds, deletes

6 STL multimap reference operator[](const key_type &k) The [] operator is not overloaded for multimap. Thus, the easy way of inserting elements, mymap[key] = value; is no longer valid, nor is the easy way of retrieving elements: value = mymap[key];

7 STL multimap Insertion requires constructing pair objects to go in the map: General: myMultimap.insert(pair (type1value,type2value)); Specific Example: myPhoneBook.insert(pair (“Turkett”,”336-758-4427”)); myPhoneBook.insert(pair (“Turkett”,”336-555-5555”));

8 STL multimap Retrieving all elements that map to a key requires use of find(), end(), and upper_bound() multimap ::iterator p; p = myPhoneBook.find(lastNameOfInterest); if (p != myPhoneBook.end()) // test if found key { do { cout first second << endl; // print name and phone number (key, then value) p++; // increment iterator } while (p != myPhoneBook.upper_bound(lastNameOfInterest)); // increment the iterator until you hit a key > lastNameOfInterest }

9 STL multimap: Available Methods iterator find(const key_type &k) Returns an iterator to the specified key. If the key is not found, an iterator to the end of the map is returned. size_type count(const key_type &k) const Returns the total number of times a key k occurs in the map

10 STL multimap: Available Methods void clear() remove all elements bool empty() returns true if empty, false otherwise size_type max_size() returns max number of elements map can hold (usually an integer returned) [capacity] size_type size() return the number of elements currently in the map (usually an integer returned) [actual size = number of total values stored, not number of unique keys]

11 STL multimap: Available Methods iterator begin() returns an iterator to the first element in the map (the first when sorted, due to storage mechanism) iterator end() returns an iterator to the last element in the map (the last when sorted) reverse_iterator rbegin() returns a reverse iterator to the end of the map reverse_iterator rend() returns a reverse iterator to the start of the map

12 STL multimap: Available Methods iterator insert(const value_type &val) Insert val (a pair) into the map, returns an iterator to the pair (which is different than for regular maps which 1) don’t allow a 2 nd insert of the same pair, and 2) give you a pair holding true or false as the 2 nd element reporting the success of insertion) iterator insert(iterator I, const value_type &val) Insert val into the map, after the value specified by i. Iterator to inserted element is returned. template void insert(InIter start, InIter end) Insert a range of elements

13 STL multimap: Available Methods void erase (iterator i) Remove the element pointed to by i. size_type erase(const key_type & k) Remove from the map all elements that have keys with the value k. void erase(iterator start, iterator end) Remove the elements in the range start to end

14 STL multimap: Available Methods iterator lower_bound(const key_type &k) Returns an iterator to the first element in the multimap with a key >= k iterator upper_bound(const key_type &k) const Returns an iterator to the first element in the multimap with a key strictly > k pair equal_range(const key_type &k) Returns a pair of iterators that point to the upper bound and the lower bound in the multimap for the specified key

15 STL multimap: Examples

16


Download ppt "STL multimap Container. STL multimaps multimaps are associative containers –Link a key to a value –AKA: Hashtables, Associative Arrays –A multimap allows."

Similar presentations


Ads by Google