다음을 통해 공유


set::find

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

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

Parameters

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

Return Value

An iterator or const_iterator that addresses the location of an element equivalent to a specified key or that addresses the location succeeding the last element in the set if no match is found for the key.

Remarks

The member function returns an iterator that addresses an element in the set 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 set object cannot be modified. If the return value of find is assigned to an iterator, the set object can be modified.

Example

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

int main( )
{
   using namespace std;
   set <int> s1;
   set <int> :: const_iterator s1_AcIter, s1_RcIter;
   
   s1.insert( 10 );
   s1.insert( 20 );
   s1.insert( 30 );

   s1_RcIter = s1.find( 20 );
   cout << "The element of set s1 with a key of 20 is: "
        << *s1_RcIter << "." << endl;

   s1_RcIter = s1.find( 40 );

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

   // The element at a specific location in the set can be found 
   // by using a dereferenced iterator addressing the location
   s1_AcIter = s1.end( );
   s1_AcIter--;
   s1_RcIter = s1.find( *s1_AcIter );
   cout << "The element of s1 with a key matching "
        << "that of the last element is: "
        << *s1_RcIter << "." << endl;
}

The element of set s1 with a key of 20 is: 20.
The set s1 doesn't have an element with a key of 40.
The element of s1 with a key matching that of the last element is: 30.

Requirements

Header: <set>

Namespace: std

See Also

Concepts

set Class

set Members

set::find (STL Samples)

Standard Template Library