set::find
傳回處理一個項目的 Iterator 的位置是索引鍵等於指定之索引鍵的集合。
iterator find(
const Key& _Key
);
const_iterator find(
const Key& _Key
) const;
參數
- _Key
項目的排序鍵會符合的引數鍵從所搜尋的設定為。
傳回值
處理項目位置等於指定按鍵或解決成功最後一個項目的位置。這個集合的列舉值或 const_iterator ,如果符合沒有索引鍵中找到。
備註
成員函式來傳回處理已在集合中的項目排序鍵與引數的索引鍵就等於在一個二進位述詞下會導致根據的排程小於可比性關聯的 Iterator。
如果 find 的傳回值指派給 const_iterator,無法修改集合物件。如果 find 的傳回值指派給 iterator,可以修改集合物件。
範例
// 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;
}
需求
標題: <set>
命名空間: std