C ++ STL set :: find()函数 (C++ STL set::find() function)
set::find() function is a predefined function, it is used to check whether an element belong to the set or not, if element finds in the set container it returns an iterator pointing to that element.
set :: find()函数是预定义的函数,用于检查元素是否属于集合,如果元素在集合容器中找到,则返回指向该元素的迭代器。
Prototype:
原型:
set<T> st; //declaration
set<T>::iterator it; //iterator declaration
it=st.find( const T item);
Parameter: const T item
参数: const T项目
Return type: Iterator position
返回类型:迭代器位置
Usage:
用法:
The function checks whether an element belong to the set or not. If an element belong to the set it returns the exact iterator position, else it returns st.end().
该功能检查元素是否属于集合。 如果元素属于集合,则它返回确切的迭代器位置,否则返回st.end() 。
Example:
例:
For a set of integer,
set<int> st;
set<int>::iterator it;
st.insert(4);
st.insert(5);
set content:
4
5
it=st.find(5);
Print *it; //prints 5
it= st.find(7) //it=st.end()
Header file to be included:
包含的头文件:
#include <iostream>
#include <set>
OR
#include <bits/stdc++.h>
C++ implementation:
C ++实现:
#include <bits/stdc++.h>
using namespace std;
void printSet(set<int> st){
set<int>:: iterator it;
cout<<"Set contents are:\n";
for(it=st.begin();it!=st.end();it++)
cout<<*it<<" ";
cout<<endl;
}
int main(){
cout<<"Example of find function\n";
set<int> st;
set<int>:: iterator it;
cout<<"inserting 4\n";
st.insert(4);
cout<<"inserting 6\n";
st.insert(6);
cout<<"inserting 10\n";
st.insert(10);
printSet(st); //printing current set
//finding element 6
if(st.find(6)!=st.end())
cout<<"6 is present\n";
else
cout<<"6 is not present\n";
//finding element 9
if(st.find(9)!=st.end())
cout<<"9 is present\n";
else
cout<<"9 is not present\n";
return 0;
}
Output
输出量
Example of find function
inserting 4
inserting 6
inserting 10
Set contents are:
4 6 10
6 is present
9 is not present
翻译自: https://www.includehelp.com/stl/set-find-function-in-cpp-stl.aspx