Constrained algorithms and algorithms on ranges(C++20) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Constrained algorithms, e.g.ranges::copy,ranges::sort, ... | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Execution policies(C++17) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Numeric operations | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Operations on uninitialized memory | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Defined in header <algorithm> | ||
template<class ForwardIt1,class ForwardIt2> bool is_permutation( ForwardIt1 first1, ForwardIt1 last1, | (1) | (since C++11) (constexpr since C++20) |
template<class ForwardIt1,class ForwardIt2, class BinaryPredicate> | (2) | (since C++11) (constexpr since C++20) |
template<class ForwardIt1,class ForwardIt2> bool is_permutation( ForwardIt1 first1, ForwardIt1 last1, | (3) | (since C++14) (constexpr since C++20) |
template<class ForwardIt1,class ForwardIt2, class BinaryPredicate> | (4) | (since C++14) (constexpr since C++20) |
Checks whether[
first1,
last1)
is apermutation of a range starting fromfirst2:
[
first2,
last2)
.IfForwardIt1
andForwardIt2
have differentvalue types, the program is ill-formed.
If the comparison function is not anequivalence relation, the behavior is undefined.
Contents |
first1, last1 | - | the pair of iterators defining the firstrange of elements to compare |
first2, last2 | - | the pair of iterators defining the secondrange of elements to compare |
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); While the signature does not need to haveconst&, the function must not modify the objects passed to it and must be able to accept all values of type (possibly const) |
Type requirements | ||
-ForwardIt1, ForwardIt2 must meet the requirements ofLegacyForwardIterator. |
true if the range[
first1,
last1)
is a permutation of the range[
first2,
last2)
,false otherwise.
Given\(\scriptsize N\)N asstd::distance(first1, last1):
ForwardIt1
andForwardIt2
are bothLegacyRandomAccessIterator, andlast1- first1!= last2- first2 istrue, no comparison will be made.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=std::next(d_first,std::distance(first, last));for(ForwardIt1 i= first; i!= last;++i){if(i!=std::find(first, i,*i))continue;// this *i has been checked auto m=std::count(d_first, d_last,*i);if(m==0||std::count(i, last,*i)!= m)returnfalse;}}returntrue;} |
Thestd::is_permutation
can be used intesting, namely to check the correctness of rearranging algorithms (e.g. sorting, shuffling, partitioning). Ifx
is an original range andy
is apermuted range thenstd::is_permutation(x, y)==true means thaty
consist of"the same" elements, maybe staying at other positions.
#include <algorithm>#include <iostream> template<typename Os,typename V>Os& operator<<(Os& os,const V& v){ os<<"{ ";for(constauto& e: v) os<< e<<' ';return os<<'}';} int main(){staticconstexprauto v1={1,2,3,4,5};staticconstexprauto v2={3,5,4,1,2};staticconstexprauto v3={3,5,4,1,1}; std::cout<< v2<<" is a permutation of "<< v1<<": "<<std::boolalpha<< std::is_permutation(v1.begin(), v1.end(), v2.begin())<<'\n'<< v3<<" is a permutation of "<< v1<<": "<< std::is_permutation(v1.begin(), v1.end(), v3.begin())<<'\n';}
Output:
{ 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 (function template)[edit] | |
generates the next smaller lexicographic permutation of a range of elements (function template)[edit] | |
(C++20) | specifies that arelation imposes an equivalence relation(concept)[edit] |
(C++20) | determines if a sequence is a permutation of another sequence (algorithm function object)[edit] |