跳到主要内容

std::ranges::is_partitioned() 算法

// (1)
constexpr bool
is_partitioned( I first, S last, Pred pred, Proj proj = {} );

// (2)
constexpr bool
is_partitioned( R&& r, Pred pred, Proj proj = {} );

参数类型是泛型的,并具有以下约束

  • I - std::input_iterator
  • S - std::sentinel_for<I>
  • R - std::ranges::input_range
  • Pred:
    • (1) - std::indirect_unary_predicate<std::projected<I, Proj>>
    • (2) - std::indirect_unary_predicate<std::projected<ranges::iterator_t<R>, Proj>>
  • Proj - (无)

对于所有重载,`Proj` 模板参数的默认类型为 `std::identity`。

  • (1) 如果范围 [first; last) 中所有在投影后满足谓词 `pred` 的元素都出现在所有不满足谓词的元素之前,则返回 `true`。

    如果 [first; last) 为空,也返回 `true`。

  • (2)(1) 相同,但使用 r 作为范围,如同使用 ranges::begin(r) 作为 firstranges::end(r) 作为 last

本页描述的函数类实体是niebloids

参数

first
last

要处理的元素范围。

r

要处理的元素范围。

pred

应用于投影元素的谓词。

proj

要应用于元素的投影。

返回值

如果范围为空或被 `p` 分区 - `true`。
否则为 false

复杂度

最多调用 `pred` 和 `proj` `ranges::distance(first, last)` 次。

异常

(无)

可能的实现

is_partitioned(1) 和 is_partitioned(2)
struct is_partitioned_fn
{
template<std::input_iterator I, std::sentinel_for<I> S, class Proj = std::identity,
std::indirect_unary_predicate<std::projected<I, Proj>> Pred>
constexpr bool operator()(I first, S last, Pred pred, Proj proj = {}) const
{
for (; first != last; ++first)
if (!std::invoke(pred, std::invoke(proj, *first)))
break;

for (; first != last; ++first)
if (std::invoke(pred, std::invoke(proj, *first)))
return false;

return true;
}

template<ranges::input_range R, class Proj = std::identity,
std::indirect_unary_predicate<std::projected<ranges::iterator_t<R>, Proj>> Pred>
constexpr bool operator()(R&& r, Pred pred, Proj proj = {}) const
{
return (*this)(ranges::begin(r), ranges::end(r), std::ref(pred), std::ref(proj));
}
};

inline constexpr auto is_partitioned = is_partitioned_fn();

示例

Main.cpp
#include <algorithm>
#include <array>
#include <iostream>
#include <numeric>
#include <utility>

int main()
{
std::array<int, 9> v;

auto print = [&v](bool o)
{
for (int x : v)
std::cout << x << ' ';
std::cout << (o ? "=> " : "=> not ") << "partitioned\n";
};

auto is_even = [](int i) { return i % 2 == 0; };

std::iota(v.begin(), v.end(), 1); // or std::ranges::iota(v, 1);
print(std::ranges::is_partitioned(v, is_even));

std::ranges::partition(v, is_even);
print(std::ranges::is_partitioned(std::as_const(v), is_even));

std::ranges::reverse(v);
print(std::ranges::is_partitioned(v.cbegin(), v.cend(), is_even));
print(std::ranges::is_partitioned(v.crbegin(), v.crend(), is_even));
}
可能的输出
1 2 3 4 5 6 7 8 9 => not partitioned
2 4 6 8 5 3 7 1 9 => partitioned
9 1 7 3 5 8 6 4 2 => not partitioned
9 1 7 3 5 8 6 4 2 => partitioned
本文档来源于此 CppReference 页面。它可能为了改进或编辑者偏好而进行了修改。点击“编辑此页面”查看本文档的所有更改。
悬停查看原始许可证。

std::ranges::is_partitioned() 算法

// (1)
constexpr bool
is_partitioned( I first, S last, Pred pred, Proj proj = {} );

// (2)
constexpr bool
is_partitioned( R&& r, Pred pred, Proj proj = {} );

参数类型是泛型的,并具有以下约束

  • I - std::input_iterator
  • S - std::sentinel_for<I>
  • R - std::ranges::input_range
  • Pred:
    • (1) - std::indirect_unary_predicate<std::projected<I, Proj>>
    • (2) - std::indirect_unary_predicate<std::projected<ranges::iterator_t<R>, Proj>>
  • Proj - (无)

对于所有重载,`Proj` 模板参数的默认类型为 `std::identity`。

  • (1) 如果范围 [first; last) 中所有在投影后满足谓词 `pred` 的元素都出现在所有不满足谓词的元素之前,则返回 `true`。

    如果 [first; last) 为空,也返回 `true`。

  • (2)(1) 相同,但使用 r 作为范围,如同使用 ranges::begin(r) 作为 firstranges::end(r) 作为 last

本页描述的函数类实体是niebloids

参数

first
last

要处理的元素范围。

r

要处理的元素范围。

pred

应用于投影元素的谓词。

proj

要应用于元素的投影。

返回值

如果范围为空或被 `p` 分区 - `true`。
否则为 false

复杂度

最多调用 `pred` 和 `proj` `ranges::distance(first, last)` 次。

异常

(无)

可能的实现

is_partitioned(1) 和 is_partitioned(2)
struct is_partitioned_fn
{
template<std::input_iterator I, std::sentinel_for<I> S, class Proj = std::identity,
std::indirect_unary_predicate<std::projected<I, Proj>> Pred>
constexpr bool operator()(I first, S last, Pred pred, Proj proj = {}) const
{
for (; first != last; ++first)
if (!std::invoke(pred, std::invoke(proj, *first)))
break;

for (; first != last; ++first)
if (std::invoke(pred, std::invoke(proj, *first)))
return false;

return true;
}

template<ranges::input_range R, class Proj = std::identity,
std::indirect_unary_predicate<std::projected<ranges::iterator_t<R>, Proj>> Pred>
constexpr bool operator()(R&& r, Pred pred, Proj proj = {}) const
{
return (*this)(ranges::begin(r), ranges::end(r), std::ref(pred), std::ref(proj));
}
};

inline constexpr auto is_partitioned = is_partitioned_fn();

示例

Main.cpp
#include <algorithm>
#include <array>
#include <iostream>
#include <numeric>
#include <utility>

int main()
{
std::array<int, 9> v;

auto print = [&v](bool o)
{
for (int x : v)
std::cout << x << ' ';
std::cout << (o ? "=> " : "=> not ") << "partitioned\n";
};

auto is_even = [](int i) { return i % 2 == 0; };

std::iota(v.begin(), v.end(), 1); // or std::ranges::iota(v, 1);
print(std::ranges::is_partitioned(v, is_even));

std::ranges::partition(v, is_even);
print(std::ranges::is_partitioned(std::as_const(v), is_even));

std::ranges::reverse(v);
print(std::ranges::is_partitioned(v.cbegin(), v.cend(), is_even));
print(std::ranges::is_partitioned(v.crbegin(), v.crend(), is_even));
}
可能的输出
1 2 3 4 5 6 7 8 9 => not partitioned
2 4 6 8 5 3 7 1 9 => partitioned
9 1 7 3 5 8 6 4 2 => not partitioned
9 1 7 3 5 8 6 4 2 => partitioned
本文档来源于此 CppReference 页面。它可能为了改进或编辑者偏好而进行了修改。点击“编辑此页面”查看本文档的所有更改。
悬停查看原始许可证。