C++ 算法 中的 is_permutation函数

首页 / C++入门教程 / C++ 算法 中的 is_permutation函数

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.

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

左耳听风 -〔陈皓〕

React实战进阶45讲 -〔王沛〕

邱岳的产品实战 -〔邱岳〕

10x程序员工作法 -〔郑晔〕

SQL必知必会 -〔陈旸〕

OAuth 2.0实战课 -〔王新栋〕

MySQL 必知必会 -〔朱晓峰〕

Web 3.0入局攻略 -〔郭大治〕

云原生架构与GitOps实战 -〔王炜〕

好记忆不如烂笔头。留下您的足迹吧 :)