Namespaces
Variants

std:: find_end

From cppreference.net
Algorithm library
Constrained algorithms and algorithms on ranges (C++20)
Constrained algorithms, e.g. ranges::copy , ranges::sort , ...
Execution policies (C++17)
Non-modifying sequence operations
Batch operations
(C++17)
Search operations
Modifying sequence operations
Copy operations
(C++11)
(C++11)
Swap operations
Transformation operations
Generation operations
Removing operations
Order-changing operations
(until C++17) (C++11)
(C++20) (C++20)
Sampling operations
(C++17)

Sorting and related operations
Partitioning operations
Sorting operations
Binary search operations
(on partitioned ranges)
Set operations (on sorted ranges)
Merge operations (on sorted ranges)
Heap operations
Minimum/maximum operations
Lexicographical comparison operations
Permutation operations
C library
Numeric operations
Operations on uninitialized memory
定义于头文件 <algorithm>
template < class ForwardIt1, class ForwardIt2 >

ForwardIt1 find_end ( ForwardIt1 first, ForwardIt1 last,

ForwardIt2 s_first, ForwardIt2 s_last ) ;
(1) (自 C++20 起为 constexpr)
template < class ExecutionPolicy, class ForwardIt1, class ForwardIt2 >

ForwardIt1 find_end ( ExecutionPolicy && policy,
ForwardIt1 first, ForwardIt1 last,

ForwardIt2 s_first, ForwardIt2 s_last ) ;
(2) (自 C++17 起)
template < class ForwardIt1, class ForwardIt2, class BinaryPred >

ForwardIt1 find_end ( ForwardIt1 first, ForwardIt1 last,
ForwardIt2 s_first, ForwardIt2 s_last,

BinaryPred p ) ;
(3) (自 C++20 起为 constexpr)
template < class ExecutionPolicy,

class ForwardIt1, class ForwardIt2, class BinaryPred >
ForwardIt1 find_end ( ExecutionPolicy && policy,
ForwardIt1 first, ForwardIt1 last,
ForwardIt2 s_first, ForwardIt2 s_last,

BinaryPred p ) ;
(4) (自 C++17 起)

在范围 [ first , last ) 中搜索序列 [ s_first , s_last ) 的最后一次出现。

1) 元素使用 operator == 进行比较。
3) 元素通过给定的二元谓词 p 进行比较。
2,4) (1,3) 相同,但根据 policy 执行。
这些重载仅在满足以下所有条件时参与重载决议:

std:: is_execution_policy_v < std:: decay_t < ExecutionPolicy >> true

(C++20 前)

std:: is_execution_policy_v < std:: remove_cvref_t < ExecutionPolicy >> true

(C++20 起)

目录

参数

first, last - 定义待检验元素范围的迭代器对
s_first, s_last - 定义待搜索元素范围的迭代器对
policy - 要使用的执行策略
p - 若元素应被视为相等则返回 ​ true 的二元谓词

谓词函数的签名应等价于如下形式:

bool pred ( const Type1 & a, const Type2 & b ) ;

虽然签名不需要包含 const & ,但函数不得修改传递给它的对象,且必须能够接受所有(可能为 const 的) Type1 Type2 类型的值,无论值类别为何(因此不允许 Type1 & ,也不允许 Type1 ,除非对于 Type1 移动操作等价于拷贝操作 (C++11 起) )。
类型 Type1 Type2 必须使得 ForwardIt1 ForwardIt2 类型的对象在解引用后能分别隐式转换到 Type1 Type2 。 ​

类型要求
-
ForwardIt1 必须满足 LegacyForwardIterator 的要求。
-
ForwardIt2 必须满足 LegacyForwardIterator 的要求。

返回值

指向范围 [ first , last ) 中最后一次出现序列 [ s_first , s_last ) 起始位置的迭代器。

如果 [ s_first , s_last ) 为空范围或未找到匹配序列,则返回 last

复杂度

给定 N std:: distance ( first, last ) S std:: distance ( s_first, s_last )

1,2) 最多使用 operator == 进行 S·(N-S+1) 次比较。
3,4) 最多 S·(N-S+1) 次谓词 p 的应用。

异常

带有名为 ExecutionPolicy 模板参数的重载按以下方式报告错误:

  • 如果作为算法一部分调用的函数执行抛出异常,且 ExecutionPolicy 是某个 标准策略 ,则会调用 std::terminate 。对于其他任何 ExecutionPolicy ,其行为由实现定义。
  • 如果算法无法分配内存,将抛出 std::bad_alloc

可能的实现

find_end (1)
template<class ForwardIt1, class ForwardIt2>
constexpr //< 自 C++20 起
ForwardIt1 find_end(ForwardIt1 first, ForwardIt1 last,
                    ForwardIt2 s_first, ForwardIt2 s_last)
{
    if (s_first == s_last)
        return last;
    ForwardIt1 result = last;
    while (true)
    {
        ForwardIt1 new_result = std::search(first, last, s_first, s_last);
        if (new_result == last)
            break;
        else
        {
            result = new_result;
            first = result;
            ++first;
        }
    }
    return result;
}
find_end (3)
template<class ForwardIt1, class ForwardIt2, class BinaryPred>
constexpr //< 自 C++20 起
ForwardIt1 find_end(ForwardIt1 first, ForwardIt1 last,
                    ForwardIt2 s_first, ForwardIt2 s_last,
                    BinaryPred p)
{
    if (s_first == s_last)
        return last;
    ForwardIt1 result = last;
    while (true)
    {
        ForwardIt1 new_result = std::search(first, last, s_first, s_last, p);
        if (new_result == last)
            break;
        else
        {
            result = new_result;
            first = result;
            ++first;
        }
    }
    return result;
}

示例

#include <algorithm>
#include <array>
#include <cmath>
#include <iostream>
auto print_result = [](auto result, const auto& v)
{
    result == v.end()
        ? std::cout << "未找到序列\n"
        : std::cout << "最后出现位置在: " << std::distance(v.begin(), result)
                    << '\n';
};
int main()
{
    const auto v = {1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4};
    for (auto const& x : {std::array{1, 2, 3}, {4, 5, 6}})
    {
        auto iter = std::find_end(v.begin(), v.end(), x.begin(), x.end()); // 重载 (1)
        print_result(iter, v);
    }
    for (auto const& x : {std::array{-1, -2, -3}, {-4, -5, -6}})
    {
        auto iter = std::find_end(v.begin(), v.end(), x.begin(), x.end(), // 重载 (3)
                                  [](int x, int y)
                                  {
                                      return std::abs(x) == std::abs(y);
                                  });
        print_result(iter, v);
    }
}

输出:

最后出现位置在: 8
未找到序列
最后出现位置在: 8
未找到序列

缺陷报告

下列行为变更缺陷报告被追溯应用于先前发布的 C++ 标准。

缺陷报告 适用范围 发布时行为 正确行为
LWG 1205 C++98 [ s_first , s_last ) 为空时返回值不明确 此情况下返回 last
LWG 2150 C++98 “序列出现”的条件描述有误 已修正

参见

在元素范围中搜索首个匹配序列
(函数模板)
若一个序列是另一个序列的子序列则返回 true
(函数模板)
查找首对相邻的相等元素(或满足给定谓词的元素)
(函数模板)
查找首个满足特定条件的元素
(函数模板)
搜索一组元素中的任意一个
(函数模板)
在范围中搜索首个连续出现指定次数的元素
(函数模板)
在特定范围中查找元素的最后序列
(算法函数对象)