C++算法is_permutation()函数比较两个集合中的元素,如果发现两个集合中的所有元素都匹配(即使顺序不同),则返回true值。第一个参数从[first1,last1)开始,第二个参数从first2开始。
template<class ForwardIterator1, class ForwardIterator2> bool is_permutation(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2); template<class ForwardIterator1, class ForwardIterator2, class BinaryPredicate> bool is_permutation(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator first2, BinaryPredicate pred);
first1 :它是[first1,last1)的第一个元素的输入迭代器。
last1 :它是[first1,last1)的最后一个元素的输入迭代器。
first2 :它是[first2,last2)的第一个元素的输入迭代器。
pred :它是一个二进制函数,接受两个元素作为参数并执行该函数设计的任务。
如果两个集合中的所有元素都匹配(即使顺序不同),该函数也会返回true值,否则返回false。
#include<iostream> #include<algorithm> #include<array> int main() { std::array<int, 5> a={6,7,8,9,10}; std::array<int, 5> b={10,8,7,6,9}; if(std::is_permutation(a.begin(),a.end(),b.begin())) std::cout<<"a and b have same elements.\n"; return 0; }
输出:
a and b have same elements.
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)