Algorithm Equal Function - C++ Programming Tutorial
C++ Course / STL Algorithm / Algorithm Equal Function

Algorithm Equal Function

BLUF: Mastering Algorithm Equal 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 Equal Function

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

The equal function in C++ Algorithm compares the elements in two containers and returns true if all elements in both containers match. The first range is denoted by [first1, last1) and the second range starts from first2.

Syntax

Example

template<class InputIterator1, class InputIterator2> bool equal(InputIterator1 first1, InputIterator1 last1,InputIterator2 first2);
template<class InputIterator1, class InputIterator2, class BinaryPredicate> bool equal(InputIterator1 first1, InputIterator1 last1, InputIterator2 first1, BinaryPredicate pred);

Parameter

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

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

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

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

Return value

The function will output true if all elements in both containers are identical, otherwise it will return false.

Example 1

Example

#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
bool newpredicate(int m, int n)
{
	return(m==n);
}
int main()
{
	int newints[]={20,40,60,80,100};
	std::vector<int> newvector(newints, newints+5);
	if(std::equal(newvector.begin(),newvector.end(),newints))
	std::cout<<"Both the containers have matching elements.\n";
	else
	std::cout<<"Both the containers have difference elements.\n";
	newvector[3]=81;
	if(std::equal(newvector.begin(),newvector.end(),newints,newpredicate))
	std::cout<<"Both the containers have equal containers.\n";
	else
	std::cout<<"Both the containers do not have equal elements. \n";
	return 0;
}

Output:

Output

Both the containers have matching elements.
Both the containers do not have equal elements.

Example 2

Example

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int u1[]={10,20,30,40,50};
	std::vector<int> vec_1(u1,u1+sizeof(u1)/sizeof(int));
	std::cout<<"The vector consists of:";
	for(unsigned int k=0; k<vec_1.size(); k++)
	std::cout<<" "<<vec_1[k];
	std::cout<<"\n";
	if(std::equal(vec_1.begin(),vec_1.end(),u1))
	std::cout<<"Both the containers have equal elements.\n";
	else
	cout<<"Both containers have different elements.";
}

Output:

Output

The vector consists of: 10, 20,30,40,50
Both the containers have equal elements.

Complexity

The function exhibits 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 its arguments also raises one.

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