C++ Map Library - find() Function



Description

The C++ function std::multimap::find() finds an element associated with key k.

If operation succeeds then methods returns iterator pointing to the element otherwise it returns an iterator pointing the multimap::end(). Please note that this method returns an iterator which points to the single element. To obtain entire range of equivalent elements, refer multimap::equal_range() method.

Declaration

Following is the declaration for std::multimap::find() function form std::map header.

C++98

iterator find (const key_type& k);
const_iterator find (const key_type& k) const;

Parameters

k − Key to be searched.

Return value

If object is constant qualified then method returns a constant iterator otherwise non-constant iterator.

Exceptions

No effect on container if exception is thrown.

Time complexity

Logarithmic i.e. O(log n)

Example

The following example shows the usage of std::multimap::find() function.

#include <iostream>
#include <map>

using namespace std;

int main(void) {
   /* Multimap with duplicates */
   multimap<char, int> m {
            {'a', 1},
            {'a', 2},
            {'b', 3},
            {'c', 4},
            {'c', 5},
         };

   auto pos = m.find('a');

   cout << pos->first << " = " << pos->second << endl;

   return 0;
}

Let us compile and run the above program, this will produce the following result −

a = 1
map.htm
Advertisements