std::find、std::find_if、std::find_if_not 用法

std::find

描述:
       查找容器中指定元素的首个位置。

定义:

template< class InputIt, class T >
InputIt find( InputIt first, InputIt last, const T& value );

template< class InputIt, class T >
constexpr InputIt find( InputIt first, InputIt last, const T& value );

可能的实现:

template<class InputIt, class T>
InputIt find(InputIt first, InputIt last, const T& value)
{
    for (; first != last; ++first) {
        if (*first == value) {
            return first;
        }
    }
    return last;
}

参数:
       first, last - 要检验的元素范围
       value - 要与元素比较的值

返回值:
       返回首个满足条件的迭代器,或若找不到这种元素则为 last 。

示例:

#include <iostream>
#include <algorithm>
#include <string>

int main()
{
	std::string s1 = "Hello World!";

	auto iter = std::find(s1.begin(), s1.end(), 'a');
	if (iter == std::end(s1))
	{
		std::cout << "不存在该元素" << std::endl;
	}
	else
	{
		int nIndex = std::distance(s1.begin(), iter);
		std::cout << "元素索引:" << nIndex << std::endl;//元素索引:6
	}

	return 0;
}

std::find_if

描述:
       查找容器中满足条件元素的首个位置。

定义:

template< class InputIt, class UnaryPredicate >
InputIt find_if( InputIt first, InputIt last,
                 UnaryPredicate p );

template< class InputIt, class UnaryPredicate >
constexpr InputIt find_if( InputIt first, InputIt last,
                           UnaryPredicate p );

可能的实现:

template<class InputIt, class UnaryPredicate>
InputIt find_if(InputIt first, InputIt last, UnaryPredicate p)
{
    for (; first != last; ++first) {
        if (p(*first)) {
            return first;
        }
    }
    return last;
}

参数:
       first, last - 要检验的元素范围
       value - 要与元素比较的值
       p - 若为要求的元素则返回 ​true 的一元谓词。

返回值:
       返回首个满足条件的迭代器,或若找不到这种元素则为 last 。

示例:

#include <iostream>
#include <algorithm>
#include <vector>

int main()
{
	std::vector<int> v = {0,1,2,3,4,5};

	auto iter = std::find_if(v.begin(), v.end(), [](int nValue)
	{
		return nValue >= 3;//查找值大于等于3的元素
	});
	if (iter == std::end(v))
	{
		std::cout << "不存在该元素" << std::endl;
	}
	else
	{
		int nIndex = std::distance(std::begin(v), iter);
		std::cout << "元素索引:" << nIndex << std::endl;//元素索引:3
	}

	return 0;
}

std::find_if_not

描述:
       查找容器中满足条件元素的首个位置。

定义:

template< class InputIt, class UnaryPredicate >
InputIt find_if_not( InputIt first, InputIt last,
                     UnaryPredicate q );

template< class InputIt, class UnaryPredicate >
constexpr InputIt find_if_not( InputIt first, InputIt last,
                               UnaryPredicate q );

可能的实现:

template<class InputIt, class UnaryPredicate>
InputIt find_if_not(InputIt first, InputIt last, UnaryPredicate q)
{
    for (; first != last; ++first) {
        if (!q(*first)) {
            return first;
        }
    }
    return last;
}

参数:

       first, last - 要检验的元素范围
       value - 要与元素比较的值
       q - 若为要求的元素则返回 ​false 的一元谓词。

返回值:
       返回首个满足条件的迭代器,或若找不到这种元素则为 last 。

示例:

#include <iostream>
#include <algorithm>
#include <vector>

int main()
{
	std::vector<int> v = {0,1,2,3,4,5};

	auto iter = std::find_if_not(v.begin(), v.end(), [](int nValue)
	{
		return nValue >= 3;//查找值小于3的元素
	});
	if (iter == std::end(v))
	{
		std::cout << "不存在该元素" << std::endl;
	}
	else
	{
		int nIndex = std::distance(std::begin(v), iter);
		std::cout << "元素索引:" << nIndex << std::endl;//元素索引:0
	}

	return 0;
}
  • 4
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值