std::ranges::search_n() 算法
- 自 C++20 起
- 简化
- 详细
// (1)
constexpr ranges::subrange<I>
search_n( I first, S last, std::iter_difference_t<I> count,
const T& value, Pred pred = {}, Proj proj = {} );
// (2)
constexpr ranges::borrowed_subrange_t<R>
search_n( R&& r, ranges::range_difference_t<R> count,
const T& value, Pred pred = {}, Proj proj = {} );
参数类型是泛型的,并具有以下约束
I
-std::forward_iterator
S
-std::sentinel_for<I>
T
- (无)Pred
- (无)Proj
- (无)- (2) -
R
-std::ranges::forward_range
对于所有重载,Proj
模板参数的默认类型为 std::identity
。
此外,每个重载都有以下约束
- (1) -
indirectly_comparable<I, const T*, Pred, Proj>
- (2) -
indirectly_comparable<ranges::iterator_t<R>, const T*, Pred, Proj>
(为方便阅读,此处省略了 std::
命名空间)
// (1)
template<
std::forward_iterator I,
std::sentinel_for<I> S,
class T,
class Pred = ranges::equal_to,
class Proj = std::identity
>
requires std::indirectly_comparable<I, const T*, Pred, Proj>
constexpr ranges::subrange<I>
search_n( I first, S last, std::iter_difference_t<I> count,
const T& value, Pred pred = {}, Proj proj = {} );
// (2)
template<
ranges::forward_range R,
class T,
class Pred = ranges::equal_to,
class Proj = std::identity
>
requires std::indirectly_comparable<ranges::iterator_t<R>, const T*, Pred, Proj>
constexpr ranges::borrowed_subrange_t<R>
search_n( R&& r, ranges::range_difference_t<R> count,
const T& value, Pred pred = {}, Proj proj = {} );
在给定范围内搜索第一个由 count
个相同元素组成的序列,每个元素都等于给定的 value
。
-
(1) 根据二元谓词
pred
,在范围 [first
;last
) 中搜索第一个由count
个元素组成的序列,这些元素的投影值都等于给定的value
。 -
(2) 与 (1) 相同,但使用
r
作为源范围,如同使用ranges::begin(r)
作为first
和ranges::end(r)
作为last
。
本页描述的函数类实体是niebloids。
参数
first last | 要检查的元素范围。 |
r | 要检查的元素范围。 |
count | 要搜索的序列的长度。 |
值 | 要搜索的值。 |
pred | 将投影元素与 |
proj | 应用于元素的投影。 |
返回值
-
(1) 类型为
ranges::subrange<I>
的值,表示第一个出现的count
个value
组成的所需序列。如果没有找到这样的子序列,则返回
std::ranges::subrange{ last, last }
。
如果count <= 0
,则返回std::ranges::subrange{ first, first }
。 -
(2) 与 (1) 相同,除了返回类型是
ranges::borrowed_subrange_t<R>
。
复杂度
给定 N
为 ranges::distance(first, last)
谓词和投影最多应用 N 次。
异常
(无)
可能的实现
search_n(1)
struct search_n_fn
{
template<std::forward_iterator I, std::sentinel_for<I> S, class T,
class Pred = ranges::equal_to, class Proj = std::identity>
requires std::indirectly_comparable<I, const T*, Pred, Proj>
constexpr ranges::subrange<I>
operator()(I first, S last, std::iter_difference_t<I> count,
const T& value, Pred pred = {}, Proj proj = {}) const
{
if (count <= 0)
return {first, first};
for (; first != last; ++first)
{
if (std::invoke(pred, std::invoke(proj, *first), value))
{
I start = first;
std::iter_difference_t<I> n{1};
for (;;)
{
if (n++ == count)
return {start, std::next(first)}; // found
if (++first == last)
return {first, first}; // not found
if (!std::invoke(pred, std::invoke(proj, *first), value))
break; // not equ to value
}
}
}
return {first, first};
}
template<ranges::forward_range R, class T, class Pred = ranges::equal_to,
class Proj = std::identity>
requires std::indirectly_comparable<ranges::iterator_t<R>, const T*, Pred, Proj>
constexpr ranges::borrowed_subrange_t<R>
operator()(R&& r, ranges::range_difference_t<R> count,
const T& value, Pred pred = {}, Proj proj = {}) const
{
return (*this)(ranges::begin(r), ranges::end(r),
std::move(count), value,
std::move(pred), std::move(proj));
}
};
inline constexpr search_n_fn search_n {};
备注
如果迭代器模型 std::random_access_iterator
,实现可以平均提高搜索效率。
示例
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <string>
int main()
{
static constexpr auto nums = {1, 2, 2, 3, 4, 1, 2, 2, 2, 1};
constexpr int count {3};
constexpr int value {2};
typedef int count_t, value_t;
constexpr auto result1 = std::ranges::search_n(
nums.begin(), nums.end(), count, value
);
static_assert( // found
result1.size() == count &&
std::distance(nums.begin(), result1.begin()) == 6 &&
std::distance(nums.begin(), result1.end()) == 9
);
constexpr auto result2 = std::ranges::search_n(nums, count, value);
static_assert( // found
result2.size() == count &&
std::distance(nums.begin(), result2.begin()) == 6 &&
std::distance(nums.begin(), result2.end()) == 9
);
constexpr auto result3 = std::ranges::search_n(nums, count, value_t{5});
static_assert( // not found
result3.size() == 0 &&
result3.begin() == result3.end() &&
result3.end() == nums.end()
);
constexpr auto result4 = std::ranges::search_n(nums, count_t{0}, value_t{1});
static_assert( // not found
result4.size() == 0 &&
result4.begin() == result4.end() &&
result4.end() == nums.begin()
);
constexpr char symbol {'B'};
auto to_ascii = [](const int z) -> char { return 'A' + z - 1; };
auto is_equ = [](const char x, const char y) { return x == y; };
std::cout << "Find a sub-sequence " << std::string(count, symbol) << " in the ";
std::ranges::transform(nums, std::ostream_iterator<char>(std::cout, ""), to_ascii);
std::cout << '\n';
auto result5 = std::ranges::search_n(nums, count, symbol, is_equ, to_ascii);
if (not result5.empty())
std::cout << "Found at position "
<< std::ranges::distance(nums.begin(), result5.begin()) << '\n';
}
Find a sub-sequence BBB in the ABBCDABBBA
Found at position 6