multiset::find

Returns an iterator addressing the first location of an element in a multiset that has a key equivalent to a specified key.

iterator find(
   const Key& _Key
);
const_iterator find(
   const Key& _Key
) const;

Parameters

  • _Key
    The key to be matched by the sort key of an element from the multiset being searched.

Return Value

An iterator or const_iterator that addresses the first location of an element with a specified key, or the location succeeding the last element in the multiset if no match is found for the key.

Remarks

The member function returns an iterator that addresses an element in the multiset whose sort key is equivalent to the argument key under a binary predicate that induces an ordering based on a less than comparability relation.

If the return value of find is assigned to a const_iterator, the multiset object cannot be modified. If the return value of find is assigned to a iterator, the multiset object can be modified.

Example

// multiset_find.cpp
// compile with: /EHsc
#include <set>
#include <iostream>

int main( )
{
   using namespace std;   
   multiset <int> ms1;
   multiset <int> :: const_iterator ms1_AcIter, ms1_RcIter;
   
   ms1.insert( 10 );
   ms1.insert( 20 );
   ms1.insert( 20 );

   ms1_RcIter = ms1.find( 20 );
   cout << "The first element of multiset ms1 with a key of 20 is: "
        << *ms1_RcIter << "." << endl;

   ms1_RcIter = ms1.find( 40 );

   // If no match is found for the key, end( ) is returned
   if ( ms1_RcIter == ms1.end( ) )
      cout << "The multiset ms1 doesn't have an element "
              << "with a key of 40." << endl;
   else
      cout << "The element of multiset ms1 with a key of 40 is: "
           << *ms1_RcIter << "." << endl;

   // The element at a specific location in the multiset can be
   // found using a dereferenced iterator addressing the location
   ms1_AcIter = ms1.end( );
   ms1_AcIter--;
   ms1_RcIter = ms1.find( *ms1_AcIter );
   cout << "The first element of ms1 with a key matching" << endl
        << "that of the last element is: "
        << *ms1_RcIter << "." << endl;

   // Note that the first element with a key equal to
   // the key of the last element is not the last element
   if ( ms1_RcIter == --ms1.end( ) )
      cout << "This is the last element of multiset ms1."
           << endl;
   else
      cout << "This is not the last element of multiset ms1."
           << endl;
}
The first element of multiset ms1 with a key of 20 is: 20.
The multiset ms1 doesn't have an element with a key of 40.
The first element of ms1 with a key matching
that of the last element is: 20.
This is not the last element of multiset ms1.

Requirements

Header: <set>

Namespace: std

See Also

Reference

multiset Class

Standard Template Library

Other Resources

multiset Members