Algorithm Is Permutation Function - C++ Programming Tutorial
C++ Course / STL Algorithm / Algorithm Is Permutation Function

Algorithm Is Permutation Function

BLUF: Mastering Algorithm Is Permutation Function is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Algorithm Is Permutation Function

C++ is renowned for its efficiency. Learn how Algorithm Is Permutation Function enables low-level control and high-performance computing in the tutorial below.

The is_permutation function in C++ Algorithm compares the elements within two containers. It returns true if all elements in both containers match, even if they are in a different order. The function operates on the range [first1, last1) for the first container and starts from first2 for the second container.

Syntax

Example

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

Parameter

It serves as an input iterator pointing to the initial element within the range of [first1, last1).

It serves as an input iterator pointing to the final element within the range from the initial element to the element before the last one.

It serves as an input iterator pointing to the initial element within the [first2, last2) range.

pred : It is a binary operation that takes two elements as parameters and executes the specified task.

Return value

The function will output true if all elements in both containers match, even if they are in a different order; otherwise, it will return false.

Example

Example

#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;
}

Output:

Output

a and b have same elements.

Complexity

The function exhibits a linear time complexity from the initial element to the final element.

Data races

Objects in both ranges are accessed.

Exceptions

The function will raise an exception if any of the arguments also raises an exception.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience