跳到主要内容

std::ranges::is_permutation() 算法

// (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) 如果存在一个排列,使得范围 [first1; last1) 的元素在应用相应的投影 Proj1Proj2 并使用二元谓词 Pred 作为比较器后,与范围 [first2; last2) 相等,则返回 true。否则,返回 false

  • (2)(1) 相同,但使用 r1 作为第一个源范围,r2 作为第二个源范围,如同使用 ranges::begin(r1) 作为 first1ranges::end(r1) 作为 last1ranges::begin(r2) 作为 first2ranges::end(r2) 作为 last2

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

参数

first1
last1

要处理的第一个元素范围。

r

要处理的第一个元素范围。

first2
last2

要处理的第二个元素范围。

r

要处理的第二个元素范围。

pred

要应用于投影元素的谓词。

proj

要应用于元素的投影。

返回值

如果范围 [first1; last1) 是范围 [first2; last2) 的一个排列,则为 true

复杂度

给定 Nranges::distance(first1, last1)

最多 O(N2) 次谓词和每个投影的应用,如果序列已经相等,则正好 N 次。

然而,如果 ranges::distance(first1, last1) != ranges::distance(first2, last2),则不进行谓词和投影的应用。

异常

(无)

可能的实现

is_permutation(1) 和 is_permutation(2)
struct is_permutation_fn
{
template<std::forward_iterator I1, std::sentinel_for<I1> S1,
std::forward_iterator I2, std::sentinel_for<I2> S2,
class Proj1 = std::identity, class Proj2 = std::identity,
std::indirect_equivalence_relation<std::projected<I1, Proj1>,
std::projected<I2, Proj2>>
Pred = ranges::equal_to>
constexpr bool operator()(I1 first1, S1 last1, I2 first2, S2 last2,
Pred pred = {}, Proj1 proj1 = {}, Proj2 proj2 = {}) const
{
// skip common prefix
auto ret = std::ranges::mismatch(first1, last1, first2, last2,
std::ref(pred), std::ref(proj1), std::ref(proj2));
first1 = ret.in1, first2 = ret.in2;

// iterate over the rest, counting how many times each element
// from [first1, last1) appears in [first2, last2)
for (auto i {first1}; i != last1; ++i)
{
const auto i_proj {std::invoke(proj1, *i)};
auto i_cmp = [&]<typename T>(T&& t)
{
return std::invoke(pred, i_proj, std::forward<T>(t));
};

if (i != ranges::find_if(first1, i, i_cmp, proj1))
continue; // this *i has been checked

if (const auto m {ranges::count_if(first2, last2, i_cmp, proj2)};
m == 0 or m != ranges::count_if(i, last1, i_cmp, proj1))
return false;
}
return true;
}

template<ranges::forward_range R1, ranges::forward_range R2,
class Proj1 = std::identity, class Proj2 = std::identity,
std::indirect_equivalence_relation<std::projected<ranges::iterator_t<R1>, Proj1>,
std::projected<ranges::iterator_t<R2>, Proj2>>
Pred = ranges::equal_to>
constexpr bool operator()(R1&& r1, R2&& r2, Pred pred = {},
Proj1 proj1 = {}, Proj2 proj2 = {}) const
{
return (*this)(ranges::begin(r1), ranges::end(r1),
ranges::begin(r2), ranges::end(r2),
std::move(pred), std::move(proj1), std::move(proj2));
}
};

inline constexpr is_permutation_fn is_permutation {};

备注

排列关系是一种等价关系。

ranges::is_permutation 可用于测试,即检查重新排列算法(例如排序、洗牌、分区)的正确性。

如果 x 是原始范围,y 是一个已排列的范围,那么 ranges::is_permutation(x, y) == true 意味着 y 包含“相同”的元素,可能位于不同的位置。

示例

Main.cpp
#include <algorithm>
#include <array>
#include <cmath>
#include <iostream>
#include <ranges>

auto& operator<<(auto& os, std::ranges::forward_range auto const& v)
{
os << "{ ";
for (auto const& e : v) os << e << ' ';
return os << "}";
}

int main()
{
static constexpr auto r1 = {1, 2, 3, 4, 5};
static constexpr auto r2 = {3, 5, 4, 1, 2};
static constexpr auto r3 = {3, 5, 4, 1, 1};

static_assert(
std::ranges::is_permutation(r1, r1) &&
std::ranges::is_permutation(r1, r2) &&
std::ranges::is_permutation(r2, r1) &&
std::ranges::is_permutation(r1.begin(), r1.end(), r2.begin(), r2.end()));

std::cout
<< std::boolalpha
<< "is_permutation( " << r1 << ", " << r2 << " ): "
<< std::ranges::is_permutation(r1, r2) << '\n'
<< "is_permutation( " << r1 << ", " << r3 << " ): "
<< std::ranges::is_permutation(r1, r3) << '\n'

<< "is_permutation with custom predicate and projections: "
<< std::ranges::is_permutation(
std::array {-14, -11, -13, -15, -12}, // 1st range
std::array {'F', 'E', 'C', 'B', 'D'}, // 2nd range
[](int x, int y) { return abs(x) == abs(y); }, // predicate
[](int x) { return x + 10; }, // projection for 1st range
[](char y) { return int(y - 'A'); }) // projection for 2nd range
<< '\n';
}
可能的输出
is_permutation( { 1 2 3 4 5 }, { 3 5 4 1 2 } ): true
is_permutation( { 1 2 3 4 5 }, { 3 5 4 1 1 } ): false
is_permutation with custom predicate and projections: true
本文源自此 CppReference 页面。它可能为了改进或编辑者偏好而进行了修改。点击“编辑此页面”查看本文档的所有更改。
悬停查看原始许可证。

std::ranges::is_permutation() 算法

// (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) 如果存在一个排列,使得范围 [first1; last1) 的元素在应用相应的投影 Proj1Proj2 并使用二元谓词 Pred 作为比较器后,与范围 [first2; last2) 相等,则返回 true。否则,返回 false

  • (2)(1) 相同,但使用 r1 作为第一个源范围,r2 作为第二个源范围,如同使用 ranges::begin(r1) 作为 first1ranges::end(r1) 作为 last1ranges::begin(r2) 作为 first2ranges::end(r2) 作为 last2

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

参数

first1
last1

要处理的第一个元素范围。

r

要处理的第一个元素范围。

first2
last2

要处理的第二个元素范围。

r

要处理的第二个元素范围。

pred

要应用于投影元素的谓词。

proj

要应用于元素的投影。

返回值

如果范围 [first1; last1) 是范围 [first2; last2) 的一个排列,则为 true

复杂度

给定 Nranges::distance(first1, last1)

最多 O(N2) 次谓词和每个投影的应用,如果序列已经相等,则正好 N 次。

然而,如果 ranges::distance(first1, last1) != ranges::distance(first2, last2),则不进行谓词和投影的应用。

异常

(无)

可能的实现

is_permutation(1) 和 is_permutation(2)
struct is_permutation_fn
{
template<std::forward_iterator I1, std::sentinel_for<I1> S1,
std::forward_iterator I2, std::sentinel_for<I2> S2,
class Proj1 = std::identity, class Proj2 = std::identity,
std::indirect_equivalence_relation<std::projected<I1, Proj1>,
std::projected<I2, Proj2>>
Pred = ranges::equal_to>
constexpr bool operator()(I1 first1, S1 last1, I2 first2, S2 last2,
Pred pred = {}, Proj1 proj1 = {}, Proj2 proj2 = {}) const
{
// skip common prefix
auto ret = std::ranges::mismatch(first1, last1, first2, last2,
std::ref(pred), std::ref(proj1), std::ref(proj2));
first1 = ret.in1, first2 = ret.in2;

// iterate over the rest, counting how many times each element
// from [first1, last1) appears in [first2, last2)
for (auto i {first1}; i != last1; ++i)
{
const auto i_proj {std::invoke(proj1, *i)};
auto i_cmp = [&]<typename T>(T&& t)
{
return std::invoke(pred, i_proj, std::forward<T>(t));
};

if (i != ranges::find_if(first1, i, i_cmp, proj1))
continue; // this *i has been checked

if (const auto m {ranges::count_if(first2, last2, i_cmp, proj2)};
m == 0 or m != ranges::count_if(i, last1, i_cmp, proj1))
return false;
}
return true;
}

template<ranges::forward_range R1, ranges::forward_range R2,
class Proj1 = std::identity, class Proj2 = std::identity,
std::indirect_equivalence_relation<std::projected<ranges::iterator_t<R1>, Proj1>,
std::projected<ranges::iterator_t<R2>, Proj2>>
Pred = ranges::equal_to>
constexpr bool operator()(R1&& r1, R2&& r2, Pred pred = {},
Proj1 proj1 = {}, Proj2 proj2 = {}) const
{
return (*this)(ranges::begin(r1), ranges::end(r1),
ranges::begin(r2), ranges::end(r2),
std::move(pred), std::move(proj1), std::move(proj2));
}
};

inline constexpr is_permutation_fn is_permutation {};

备注

排列关系是一种等价关系。

ranges::is_permutation 可用于测试,即检查重新排列算法(例如排序、洗牌、分区)的正确性。

如果 x 是原始范围,y 是一个已排列的范围,那么 ranges::is_permutation(x, y) == true 意味着 y 包含“相同”的元素,可能位于不同的位置。

示例

Main.cpp
#include <algorithm>
#include <array>
#include <cmath>
#include <iostream>
#include <ranges>

auto& operator<<(auto& os, std::ranges::forward_range auto const& v)
{
os << "{ ";
for (auto const& e : v) os << e << ' ';
return os << "}";
}

int main()
{
static constexpr auto r1 = {1, 2, 3, 4, 5};
static constexpr auto r2 = {3, 5, 4, 1, 2};
static constexpr auto r3 = {3, 5, 4, 1, 1};

static_assert(
std::ranges::is_permutation(r1, r1) &&
std::ranges::is_permutation(r1, r2) &&
std::ranges::is_permutation(r2, r1) &&
std::ranges::is_permutation(r1.begin(), r1.end(), r2.begin(), r2.end()));

std::cout
<< std::boolalpha
<< "is_permutation( " << r1 << ", " << r2 << " ): "
<< std::ranges::is_permutation(r1, r2) << '\n'
<< "is_permutation( " << r1 << ", " << r3 << " ): "
<< std::ranges::is_permutation(r1, r3) << '\n'

<< "is_permutation with custom predicate and projections: "
<< std::ranges::is_permutation(
std::array {-14, -11, -13, -15, -12}, // 1st range
std::array {'F', 'E', 'C', 'B', 'D'}, // 2nd range
[](int x, int y) { return abs(x) == abs(y); }, // predicate
[](int x) { return x + 10; }, // projection for 1st range
[](char y) { return int(y - 'A'); }) // projection for 2nd range
<< '\n';
}
可能的输出
is_permutation( { 1 2 3 4 5 }, { 3 5 4 1 2 } ): true
is_permutation( { 1 2 3 4 5 }, { 3 5 4 1 1 } ): false
is_permutation with custom predicate and projections: true
本文源自此 CppReference 页面。它可能为了改进或编辑者偏好而进行了修改。点击“编辑此页面”查看本文档的所有更改。
悬停查看原始许可证。