STL Map Container

advertisement
STL multimap Container
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.
STL multimaps
• Still use #include <map>
• multimaps are templated
• Template fields (multimap<templated, type, description,
here>) 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<char,int> m;
// simple constructor, for holding
// keys of type char, values of type int
// less than is already defined for char
STL multimaps: Constructors
Same basic constructors as map:
Standard: multimap<template,fields>
mapName(const Comp &cmpfn = Comp(),
const Allocator &a = Allocator());
Copy: multimap<template,fields>
mapName(const map<Key, T, Comp,
Allocator> &anotherMap);
Copy Range:
template <class InIter>
multimap(InIter start, InIter end, const Comp
&cmpfn = Comp(), const Allocator &a =
Allocator());
STL Maps: Data Storage
• Tree structure
– logarithmic time inserts, finds, deletes
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];
STL multimap
Insertion requires constructing pair objects to go in
the map:
General:
myMultimap.insert(pair<type1,type2> (type1value,type2value));
Specific Example:
myPhoneBook.insert(pair<string,string>(“Turkett”,”336-758-4427”));
myPhoneBook.insert(pair<string,string>(“Turkett”,”336-555-5555”));
STL multimap
• Retrieving all elements that map to a key requires
use of find(), end(), and upper_bound()
multimap<string, string>::iterator p;
p = myPhoneBook.find(lastNameOfInterest);
if (p != myPhoneBook.end()) // test if found key
{
do {
cout << p->first << “ “ << p->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
}
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
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]
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
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 2nd insert
of the same pair, and 2) give you a pair
holding true or false as the 2nd 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 <class InIter> void insert(InIter start, InIter end)
Insert a range of elements
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
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<iterator, iterator> 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
STL multimap: Examples
STL multimap: Examples
Download