beautypg.com

Element comparisons, Other map operations – HP Integrity NonStop H-Series User Manual

Page 101

background image

later in this section.

The member function count() returns the number of elements that match the key value supplied
as the argument. For a map, this value is always either zero or one, whereas for a multimap it
can be any nonnegative value. If you simply want to determine whether or not a collection
contains an element indexed by a given key, using count() is often easier than using the find()
function and testing the result against the end-of-sequence iterator.

if (map_one.count(4))
cout << "contains a 4th element" << endl;

Element Comparisons

The member functions key_comp() and value_comp(), which take no arguments, return
function objects that can be used to compare elements of the key or value types. Values used in
these comparisons need not be contained in the collection, and neither function will have any
effect on the container.

if (map_two.key_comp (i, j))
cout << "element i is less than j" << endl;

Other Map Operations

Because maps and multimaps are ordered collections, and because the iterators for maps return
pairs, many of the functions described in Sections

13

and

14

are meaningless or difficult to use.

However, there are a few notable exceptions. The functions for_each(), adjacent_find(), and
accumulate() each have their own uses. In all cases it is important to remember that the
functions supplied as arguments should take a key/value pair as arguments.

This manual is related to the following products: