std::is_permutation

来自cppreference.com
TranslationBot留言 | 贡献2012年10月25日 (四) 20:00的版本 (Translated from the English version using Google Translate)
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)

<metanoindex/>

 
 
算法库
受约束算法及范围上的算法 (C++20)
包含算法例如 ranges::copy, ranges::sort, ...
执行策略 (C++17)
排序和相关操作
划分操作
排序操作
二分搜索操作(在已划分范围上)
集合操作(在有序范围上)
归并操作(在有序范围上)
堆操作
最小/最大操作
(C++11)
(C++17)
字典序比较操作
排列操作
C 库

数值运算
(C++11)                       
在未初始化内存上的操作
 
在标头 <algorithm> 定义
template< class ForwardIt1, class ForwardIt2 >
bool is_permutation( ForwardIt1 first, ForwardIt1 last,
                     ForwardIt2 d_first );
(1) (C++11 起)
template< class ForwardIt1, class ForwardIt2, class BinaryPredicate >
bool is_permutation( ForwardIt1 first, ForwardIt1 last,
                     ForwardIt2 d_first, BinaryPredicate p );
(2) (C++11 起)
。返回true的范围[first1, last1),使该范围相等的范围内开始,d_first中的元素,如果存在一个置换。第一个版本使用operator==平等,第二个版本使用的二元谓词p
原文:
Returns true if there exists a permutation of the elements in the range [first1, last1) that makes that range equal to the range beginning at d_first. The first version uses operator== for equality, the second version uses the binary predicate p
文本通过谷歌翻译机器翻译。
你可以帮忙校正和验证翻译。点击此处查看指示。

。参数。

Template:param list begin Template:param list item Template:param list item Template:param list pred2 eq Template:param list hreq Template:param list req concept Template:param list end

===。 返回值。===

true如果范围[first, last)开始的范围内的一个排列在d_first.
原文:
true if the range [first, last) is a permutation of the range beginning at d_first.
文本通过谷歌翻译机器翻译。
你可以帮忙校正和验证翻译。点击此处查看指示。

。复杂性。

。在最O(N2)应用的谓词,如果序列已经等于或完全N,其中N=std::distance(first, last).
原文:
At most O(N2) applications of the predicate, or exactly N if the sequences are already equal, where N=std::distance(first, last).
文本通过谷歌翻译机器翻译。
你可以帮忙校正和验证翻译。点击此处查看指示。

。可能的实现。

template<class ForwardIt1, class ForwardIt2>
bool is_permutation(ForwardIt1 first, ForwardIt1 last,
                    ForwardIt2 d_first)
{
   // skip common prefix
   std::tie(first, d_first) = std::mismatch(first, last, d_first);
   // iterate over the rest, counting how many times each element
   // from [first, last) appears in [d_first, d_last)
   if (first != last) {
       ForwardIt2 d_last = d_first;
       std::advance(d_last, std::distance(first, last));
       for (ForwardIt1 i = first; i != last; ++i) {
            if (i != std::find(first, i, *i)) continue; // already counted this *i

            auto m = std::count(d_first, d_last, *i);
            if (m==0 || std::count(i, last, *i) != m) {
                return false;
            }
        }
    }
    return true;
}

。为例。

#include <algorithm>
#include <vector>
#include <iostream>
int main()
{
    std::vector<int> v1{1,2,3,4,5};
    std::vector<int> v2{3,5,4,1,2};
    std::cout << "3,5,4,1,2 is a permutation of 1,2,3,4,5? "
              << std::boolalpha
              << std::is_permutation(v1.begin(), v1.end(), v2.begin()) << '\n';

    std::vector<int> v3{3,5,4,1,1};
    std::cout << "3,5,4,1,1 is a permutation of 1,2,3,4,5? "
              << std::boolalpha
              << std::is_permutation(v1.begin(), v1.end(), v3.begin()) << '\n';
}

输出:

3,5,4,1,2 is a permutation of 1,2,3,4,5? true
3,5,4,1,1 is a permutation of 1,2,3,4,5? false

。另请参阅。

Template:cpp/algorithm/dcl list next permutationTemplate:cpp/algorithm/dcl list prev permutation