std::is_permutation
提供: cppreference.com
![]() | このページは、Google 翻訳を使って英語版から機械翻訳されました。 翻訳には誤りや奇妙な言い回しがあるかもしれません。文章の上にポインタをおくと、元の文章が見れます。誤りを修正して翻訳を改善する手助けをしてください。翻訳についての説明は、ここをクリックしてください。 |
Defined in header <algorithm> | ||
template<class ForwardIt1, class ForwardIt2 > bool is_permutation( ForwardIt1 first, ForwardIt1 last, | (1) | (C++11およびそれ以降) |
template<class ForwardIt1, class ForwardIt2, class BinaryPredicate > bool is_permutation( ForwardIt1 first, ForwardIt1 last, | (2) | (C++11およびそれ以降) |
戻りtrue
[first1, last1)
で範囲の先頭に、その範囲が等しくなる範囲d_first
内の要素の順列が存在する場合。最初のバージョンは、平等のためにoperator==
を使用して、2番目のバージョンは、バイナリ述語p
を使用していますOriginal:
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
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
目次 |
[編集]パラメータ
first, last | - | 比較するための要素の範囲 Original: the range of elements to compare The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
d_first | - | 第2の範囲の始まりは、比較することができます Original: the beginning of the second range to compare The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
p | - | binary predicate which returns true if the elements should be treated as equal. The signature of the predicate function should be equivalent to the following: bool pred(const Type1 &a, const Type2 &b); The signature does not need to have const&, but the function must not modify the objects passed to it. |
型の要件 | ||
-ForwardIt1, ForwardIt2 は ForwardIterator の要求を満足しなければなりません。 |
[編集]値を返します
true範囲
[first, last)
はd_first
で範囲の先頭の順列である場合.Original:
true if the range
[first, last)
is a permutation of the range beginning at d_first
.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
[編集]複雑性
述部の中で最もO(N2)アプリケーションで、または正確にN配列が既に等しい場合、どこN=std::distance(first, last).
Original:
At most O(N2) applications of the predicate, or exactly N if the sequences are already equal, where N=std::distance(first, last).
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
[編集]可能な実装
template<class ForwardIt1, class ForwardIt2>bool is_permutation(ForwardIt1 first, ForwardIt1 last, ForwardIt2 d_first){// skip common prefixstd::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){returnfalse;}}}returntrue;} |
[編集]例
このコードを実行します
#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
[編集]参照
generates the next greater lexicographic permutation of a range of elements (関数テンプレート) | |
generates the next smaller lexicographic permutation of a range of elements (関数テンプレート) |