STL_set——set::find

来源:互联网 发布:ubuntu用终端设置中文 编辑:程序博客网 时间:2024/06/05 23:03
Reference:
Returns an iterator addressing the location of an element in a set that has a key equivalent to a specified key.

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

Parameter:
_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:
#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;}

Output:
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.
0 0
原创粉丝点击