std::find_end
来自cppreference.com
| 在标头 <algorithm> 定义
|
||
| (1) | (C++20 起为 constexpr) |
|
| |
(2) | (C++17 起) |
| (3) | (C++20 起为 constexpr) |
|
| |
(4) | (C++17 起) |
在范围 [first, last) 中搜索序列 [s_first, s_last) 最后一次出现的位置。
1) 用
operator== 比较元素。3) 用给定的二元谓词
p 比较元素。2,4) 同 (1,3),但按照
policy 执行。 这些重载只有在满足以下所有条件时才会参与重载决议:
|
|
(C++20 前) |
|
|
(C++20 起) |
参数
| first, last | - | 要检验的元素范围的迭代器对 |
| s_first, s_last | - | 要搜索的元素范围的迭代器对 |
| policy | - | 所用的执行策略 |
| p | - | 若元素应被当做相等则返回 true 的二元谓词谓词函数的签名应等价于如下:
虽然签名不必有 |
| 类型要求 | ||
-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) 最多应用 S·(N-S+1) 次
operator== 进行比较。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 (函数模板) | |
| 查找首对相同(或满足给定谓词)的相邻元素 (函数模板) | |
(C++11) |
查找首个满足特定条件的元素 (函数模板) |
| 搜索一组元素中任一元素 (函数模板) | |
| 搜索元素在范围中首次连续若干次出现 (函数模板) | |
(C++20) |
查找元素序列在特定范围中最后一次出现 (算法函数对象) |