无涯教程-C++ 算法 - is_permutation函数

29 阅读1分钟

C++算法is_permutation()函数比较两个集合中的元素,如果发现两个集合中的所有元素都匹配(即使顺序不同),则返回true值。第一个参数从[first1,last1)开始,第二个参数从first2开始。

is_permutation - 语法

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);

is_permutation - 参数

first1 :它是[first1,last1)的第一个元素的输入迭代器。

last1 :它是[first1,last1)的最后一个元素的输入迭代器。

first2 :它是[first2,last2)的第一个元素的输入迭代器。

pred :它是一个二进制函数,接受两个元素作为参数并执行该函数设计的任务。

is_permutation - 返回值

如果两个集合中的所有元素都匹配(即使顺序不同),该函数也会返回true值,否则返回false。

is_permutation - 例子

#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.

参考链接

www.learnfk.com/c++/cpp-alg…