Namespaces
Variants

std::experimental::ranges:: find, std::experimental::ranges:: find_if, std::experimental::ranges:: find_if_not

From cppreference.net
template < InputIterator I, Sentinel < I > S, class T, class Proj = ranges:: identity >

requires IndirectRelation < ranges:: equal_to <> , projected < I, Proj > , const T * >

I find ( I first, S last, const T & value, Proj proj = Proj { } ) ;
(1) (ranges TS)
template < InputRange R, class T, class Proj = ranges:: identity >

requires IndirectRelation < ranges:: equal_to <> ,
projected < ranges:: iterator_t < R > , Proj > , const T * >

ranges:: safe_iterator_t < R > find ( R && r, const T & value, Proj proj = Proj { } ) ;
(2) (ranges TS)
template < InputIterator I, Sentinel < I > S, class Proj = ranges:: identity ,

IndirectUnaryPredicate < projected < I, Proj >> Pred >

I find_if ( I first, S last, Pred pred, Proj proj = Proj { } ) ;
(3) (ranges TS)
template < InputRange R, class Proj = ranges:: identity ,

IndirectUnaryPredicate < projected < ranges:: iterator_t < R > , Proj >> Pred >

ranges:: safe_iterator_t < R > find_if ( R && r, Pred pred, Proj proj = Proj { } ) ;
(4) (ranges TS)
template < InputIterator I, Sentinel < I > S, class Proj = ranges:: identity ,

IndirectUnaryPredicate < projected < I, Proj >> Pred >

I find_if_not ( I first, S last, Pred pred, Proj proj = Proj { } ) ;
(5) (ranges TS)
template < InputRange R, class Proj = ranges:: identity ,

IndirectUnaryPredicate < projected < ranges:: iterator_t < R > , Proj >> Pred >

ranges:: safe_iterator_t < R > find_if_not ( R && r, Pred pred, Proj proj = Proj { } ) ;
(6) (ranges TS)

返回范围 [ first , last ) 中首个满足特定条件的元素:

1) find 搜索其投影值等于 value 的元素(即满足 value == ranges:: invoke ( proj, * i ) 的条件)。
3) find_if 查找满足以下条件的元素:其投影值使谓词 p 返回 true (即 ranges:: invoke ( pred, ranges:: invoke ( proj, * i ) ) true )。
5) find_if_not 查找满足以下条件的元素:其投影值的谓词 q 返回 false (即 ranges:: invoke ( pred, ranges:: invoke ( proj, * i ) ) false )。
2,4,6) (1,3,5) 相同,但使用 r 作为源范围,如同以 ranges:: begin ( r ) 作为 first ,并以 ranges:: end ( r ) 作为 last

尽管存在上述声明,算法声明的实际模板参数数量和顺序是未指定的。因此,若在调用算法时使用了显式模板参数,该程序很可能不具备可移植性。

目录

参数

first, last - 要检查的元素范围
r - 要检查的元素范围
value - 与投影元素比较的值
pred - 应用于投影元素的谓词
proj - 应用于元素的投影

返回值

指向首个满足条件的元素的迭代器。若找不到此元素,则返回与 last 相等的迭代器。

复杂度

最多 last - first 次谓词和投影函数的应用。

可能的实现

第一版本
template<InputIterator I, Sentinel<I> S, class T, class Proj = ranges::identity>
    requires IndirectRelation<ranges::equal_to<>, projected<I, Proj>, const T*>
I find(I first, S last, const T& value, Proj proj = Proj{})
{
    for (; first != last; ++first)
        if (ranges::invoke(proj, *first) == value)
            break;
    return first;
}
第二版本
template<InputIterator I, Sentinel<I> S, class Proj = ranges::identity,
         IndirectUnaryPredicate<projected<I, Proj>> Pred>
I find_if(I first, S last, Pred pred, Proj proj = Proj{})
{
    for (; first != last; ++first)
        if (ranges::invoke(pred, ranges::invoke(proj, *first)))
            break;
    return first;
}
第三版本
template<InputIterator I, Sentinel<I> S, class Proj = ranges::identity,
         IndirectUnaryPredicate<projected<I, Proj>> Pred>
I find_if_not(I first, S last, Pred pred, Proj proj = Proj{})
{
    for (; first != last; ++first)
        if (!ranges::invoke(pred, ranges::invoke(proj, *first)))
            break;
    return first;
}

示例

参见

查找首个满足特定条件的元素
(函数模板)
查找首对相邻且相等(或满足给定谓词)的元素
(函数模板)
在特定范围内查找最后的元素序列
(函数模板)
搜索一组元素中的任意一个
(函数模板)
查找两个范围首次出现差异的位置
(函数模板)
搜索一个元素范围
(函数模板)