C ++ STL中的set :: find()函数

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

  • 10
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值