std::search_n
定义于头文件 <algorithm> |
||
(1) | ||
template< class ForwardIt, class Size, class T > ForwardIt search_n( ForwardIt first, ForwardIt last, |
(constexpr since C++20) (直到 C++26) |
|
template< class ForwardIt, class Size, class T = typename std::iterator_traits |
(since C++26) | |
(2) | ||
template< class ExecutionPolicy, class ForwardIt, class Size, class T > |
(since C++17) (直到 C++26) |
|
template< class ExecutionPolicy, class ForwardIt, class Size, |
(since C++26) | |
(3) | ||
template< class ForwardIt, class Size, class T, class BinaryPred > ForwardIt search_n( ForwardIt first, ForwardIt last, |
(constexpr since C++20) (直到 C++26) |
|
template< class ForwardIt, class Size, class T = typename std::iterator_traits |
(since C++26) | |
(4) | ||
template< class ExecutionPolicy, class ForwardIt, class Size, class T, class BinaryPred > |
(since C++17) (直到 C++26) |
|
template< class ExecutionPolicy, class ForwardIt, class Size, class T = typename std::iterator_traits |
(since C++26) | |
在范围 [
first,
last)
中搜索第一个由 count 个相同元素组成的序列,每个元素都等于给定的 value。
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 | - | 定义要检查的元素的范围的迭代器对 |
count | - | 要搜索的序列的长度 |
value | - | 要搜索的元素的值 |
policy | - | 要使用的执行策略 |
p | - | 如果元素应被视为相等,则返回 true 的二元谓词。 谓词函数的签名应等效于以下内容 bool pred(const Type1 &a, const Type2 &b); 虽然签名不需要具有 const &,但该函数不得修改传递给它的对象,并且必须能够接受类型为(可能是 const) |
类型要求 | ||
-ForwardIt 必须满足 LegacyForwardIterator 的要求。 | ||
-BinaryPred 必须满足 BinaryPredicate 的要求。 | ||
-Size 必须是 可转换 为 整型。 |
[编辑] 返回值
如果 count 为正数,则返回指向在范围 [
first,
last)
中找到的第一个序列的开头的迭代器。序列中的每个迭代器 it 都应满足以下条件
如果未找到此类序列,则返回 last。
如果 count 为零或负数,则返回 first。
[编辑] 复杂度
给定 N 作为 std::distance(first, last)
[编辑] 异常
带有名为 ExecutionPolicy
的模板参数的重载按如下方式报告错误
- 如果作为算法一部分调用的函数的执行抛出异常,并且
ExecutionPolicy
是标准策略之一,则调用 std::terminate。对于任何其他ExecutionPolicy
,行为是实现定义的。 - 如果算法无法分配内存,则抛出 std::bad_alloc。
[编辑] 可能的实现
search_n (1) |
---|
template<class ForwardIt, class Size, class T = typename std::iterator_traits<ForwardIt>::value_type> ForwardIt search_n(ForwardIt first, ForwardIt last, Size count, const T& value) { if (count <= 0) return first; for (; first != last; ++first) { if (!(*first == value)) continue; ForwardIt candidate = first; for (Size cur_count = 1; true; ++cur_count) { if (cur_count >= count) return candidate; // success ++first; if (first == last) return last; // exhausted the list if (!(*first == value)) break; // too few in a row } } return last; } |
search_n (3) |
template<class ForwardIt, class Size, class T = typename std::iterator_traits<ForwardIt>::value_type, class BinaryPred> ForwardIt search_n(ForwardIt first, ForwardIt last, Size count, const T& value, BinaryPred p) { if (count <= 0) return first; for (; first != last; ++first) { if (!p(*first, value)) continue; ForwardIt candidate = first; for (Size cur_count = 1; true; ++cur_count) { if (cur_count >= count) return candidate; // success ++first; if (first == last) return last; // exhausted the list if (!p(*first, value)) break; // too few in a row } } return last; } |
[编辑] 注解
特性测试 宏 | 值 | Std | 特性 |
---|---|---|---|
__cpp_lib_algorithm_default_value_type |
202403 |
(C++26) | 列表初始化 用于算法 (1-4) |
[编辑] 示例
#include <algorithm> #include <cassert> #include <complex> #include <iostream> #include <iterator> #include <vector> template<class Container, class Size, class T> constexpr bool consecutive_values(const Container& c, Size count, const T& v) { return std::search_n(std::begin(c), std::end(c), count, v) != std::end(c); } int main() { constexpr char sequence[] = ".0_0.000.0_0."; static_assert(consecutive_values(sequence, 3, '0')); for (int n : {4, 3, 2}) std::cout << std::boolalpha << "Has " << n << " consecutive zeros: " << consecutive_values(sequence, n, '0') << '\n'; std::vector<std::complex<double>> nums{{4, 2}, {4, 2}, {1, 3}}; #ifdef __cpp_lib_algorithm_default_value_type auto it = std::search_n(nums.cbegin(), nums.cend(), 2, {4, 2}); #else auto it = std::search_n(nums.cbegin(), nums.cend(), 2, std::complex<double>{4, 2}); #endif assert(it == nums.begin()); }
输出
Has 4 consecutive zeros: false Has 3 consecutive zeros: true Has 2 consecutive zeros: true
[编辑] 缺陷报告
以下行为更改缺陷报告被追溯应用于先前发布的 C++ 标准。
DR | 应用于 | 已发布的行为 | 正确的行为 |
---|---|---|---|
LWG 283 | C++98 | T 被要求是 EqualityComparable,但是InputIt 的值类型并不总是 T |
移除了要求 |
LWG 426 | C++98 | 复杂度上限为 N·count ,如果 count 为负数,则为负数 |
上限为 0 如果 count 为非正数 |
LWG 714 | C++98 | 如果 count > 0,复杂度上限为 N·count ,但在最坏情况下,比较/操作的次数始终为 N |
更改了上限 在这种情况下限制为 N |
LWG 2150 | C++98 | “序列出现”的条件不正确 | 已更正 |
[编辑] 参见
查找某个范围内元素的最后一个序列 (函数模板) | |
(C++11) |
查找满足特定条件的第一个元素 (函数模板) |
搜索元素范围的首次出现 (函数模板) | |
(C++20) |
在范围内搜索元素的若干个连续副本的首次出现 (算法函数对象) |