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.

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

技术教程推荐

如何做好一场技术演讲 -〔极客时间〕

Nginx核心知识150讲 -〔陶辉〕

网络编程实战 -〔盛延敏〕

即时消息技术剖析与实战 -〔袁武林〕

DDD实战课 -〔欧创新〕

张汉东的Rust实战课 -〔张汉东〕

成为AI产品经理 -〔刘海丰〕

打造爆款短视频 -〔周维〕

Midjourney入门实践课 -〔Jovi〕

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