Iterators, Searching and counting – HP Integrity NonStop H-Series User Manual
Page 100

// erase the 4th element 4
map_three.erase(4);
// erase the 5th element
mtesttype::iterator five = map_three.find(5);
map_three.erase(five);
// erase all values between the 7th and 11th elements
mtesttype::iterator seven = map_three.find(7);
mtesttype::iterator eleven = map_three.find(11);
map_three.erase (seven, eleven);
If the underlying element type provides a destructor, then the destructor will be invoked prior to
removing the key and value pair from the collection.
Iterators
No Iterator Invalidation
The member functions begin() and end() produce bidirectional iterators for both maps and
multimaps. Dereferencing an iterator for either a map or a multimap will yield a
pair
of
key/value elements. The field names first and second can be applied to these values to access
the individual fields. The first field is constant, and cannot be modified. The second field,
however, can be used to change the value being held in association with a given key. Elements
will be generated in sequence, based on the ordering of the key fields.
The member functions rbegin() and rend() produce iterators that yield the elements in reverse
order.
Searching and Counting
The member function size() will yield the number of elements held by a container. The member
function empty() will return a boolean true value if the container is empty, and is generally
faster than testing the size against zero.
The member function find() takes a key argument, and returns an iterator denoting the
associated key/value pair. In the case of multimaps, the first such value is returned. In both
cases the past-the-end iterator is returned if no such value is found.
if (map_one.find(4) != map_one.end())
cout << "contains a 4th element" << endl;
The member function lower_bound() yields the first entry that matches the argument key, while
the member function upper_bound() returns the first value past the last entry matching the
argument. Finally, the member function equal_range() returns a
pair
of iterators, holding the
lower and upper bounds. An example showing the use of these procedures will be presented