TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C++ algorithm equal() function

C++ algorithm equal() function tutorial for beginners and professionals with examples on adjacent_find(),any_of(), copy(), copy_if(), count(), count_if(), equal(), find(), find_end(), find_first_of(), find_if(), find_if_not(), for_each() etc.

<< Back to CPP

C++ Algorithm Function equal()

C++ Algorithm equal()function compares the elements in both the containers and returns a true value if all the elements in both the containers are found to be matching. The first range is from [first1,last1) and the second starts from first2.

Syntax

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

first1: It is an input iterator to the first element of the [first1, last1).

last1: It is an input iterator to the last element of the [first1, last1).

first2: It is an input iterator to the first element of the [first2, last2).

pred: It is a binary function that accepts two elements as arguments and performs the task designed by the function.

Return value

The function returns the value true if all the elements in both the containers match, otherwise it returns false.

Example 1

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

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

Example 2

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

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

Complexity

The function has linear complexity from the first1 element to the last1 element.

Data races

Objects in both ranges are accessed.

Exceptions

The function throws an exception if any of the argument throws one.






Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf