std::find_end
定义于头文件 <algorithm> |
||
template< class ForwardIt1, class ForwardIt2 > ForwardIt1 find_end( ForwardIt1 first, ForwardIt1 last, |
(1) | (constexpr since C++20) |
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2 > ForwardIt1 find_end( ExecutionPolicy&& policy, |
(2) | (since C++17) |
template< class ForwardIt1, class ForwardIt2, class BinaryPred > ForwardIt1 find_end( ForwardIt1 first, ForwardIt1 last, |
(3) | (constexpr since C++20) |
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class BinaryPred > |
(4) | (since C++17) |
在范围 [
first,
last)
中搜索序列 [
s_first,
s_last)
的最后一次出现。
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) |
类型要求 | ||
-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)
[编辑] 异常
带有名为 ExecutionPolicy
的模板形参的重载按如下方式报告错误
- 若作为算法一部分调用的函数抛出异常,且
ExecutionPolicy
为标准策略之一,则调用 std::terminate。对于任何其他ExecutionPolicy
,行为是实现定义的。 - 若算法无法分配内存,则抛出 std::bad_alloc。
[编辑] 可能的实现
find_end (1) |
---|
template<class ForwardIt1, class ForwardIt2> constexpr //< since 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 //< since 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 << "Sequence not found\n" : std::cout << "Last occurrence is at: " << 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()); // overload (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(), // overload (3) [](int x, int y) { return std::abs(x) == std::abs(y); }); print_result(iter, v); } }
输出
Last occurrence is at: 8 Sequence not found Last occurrence is at: 8 Sequence not found
[编辑] 缺陷报告
下列更改行为的缺陷报告追溯性地应用于以前发布的 C++ 标准。
DR | 应用于 | 发布时的行为 | 正确行为 |
---|---|---|---|
LWG 1205 | C++98 | 若 [ s_first, s_last) 为空,则返回值不明确 |
在此情况下返回 last |
LWG 2150 | C++98 | “序列出现”的条件不正确 | 已更正 |
[编辑] 参见
search (函数模板) | |
includes (函数模板) | |
adjacent_find (函数模板) | |
(C++11) |
find (函数模板) |
find_first_of (函数模板) | |
search_n (函数模板) | |
find_end (算法函数对象) |