std::equal() helps to compares the elements within the range [first_1,last_1) with those within range beginning at first_2. Syntax 1:
template
bool equal (InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2)
first_1, last_1 : Initial and final positions of the first
sequence. All the elements are present within a range [first_1,last_1)
first2 : Initial position of the second sequence.
Returns :
true, if all of the elements in both ranges match; otherwise false
CPP
// C++ program illustrating
// use of bool equal (InputIterator1 first1, InputIterator1 last1,
// InputIterator2 first2)
#include <bits/stdc++.h>
int main()
{
int v1[] = { 10, 20, 30, 40, 50 };
std::vector<int> vector_1 (v1, v1 + sizeof(v1) / sizeof(int) );
// Printing vector1
std::cout << "Vector contains : ";
for (unsigned int i = 0; i < vector_1.size(); i++)
std::cout << " " << vector_1[i];
std::cout << "\n";
// using std::equal()
// Comparison within default constructor
if ( std::equal (vector_1.begin(), vector_1.end(), v1) )
std::cout << "The contents of both sequences are equal.\n";
else
printf("The contents of both sequences differ.");
}
Output:
Vector contains : 10, 20, 30, 40, 50
The contents of both sequences are equal.
Syntax 2:
template
bool equal (InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, BinaryPredicate pred);
first_1, last_1 : Initial and final positions of the first
sequence. All the elements are present within a range [first_1,last_1)
first2 : Initial position of the second sequence.
pred : Binary function that accepts two elements as argument
and returns a value convertible to boolean.
Returns :
true, if all of the elements in both ranges match; otherwise false
CPP
// C++ program illustrating
// use of bool equal (InputIterator1 first1, InputIterator1 last1,
// InputIterator2 first2, BinaryPredicate pred);
#include <bits/stdc++.h>
bool pred(int i, int j)
{
return (i != j);
}
int main()
{
int v1[] = { 10, 20, 30, 40, 50 };
std::vector<int> vector_1 (v1, v1 + sizeof(v1) / sizeof(int) );
// Printing vector1
std::cout << "Vector contains : ";
for (unsigned int i = 0; i < vector_1.size(); i++)
std::cout << " " << vector_1[i];
std::cout << "\n";
// using std::equal()
// Comparison based on pred
if ( std::equal (vector_1.begin(), vector_1.end(), v1, pred) )
std::cout << "The contents of both sequences are equal.\n";
else
printf("The contents of both sequences differ.");
}
Output:
Vector contains : 10, 20, 30, 40, 50
The contents of both sequences differ.
Time complexity: O(n)
Related Articles:
Similar Reads
equal_range in C++ std::equal_range is used to find the sub-range within a given range [first, last) that has all the elements equivalent to a given value. It returns the initial and the final bound of such a sub-range. This function requires the range to be either sorted or partitioned according to some condition suc
5 min read
map equal_range() in C++ STL The map::equal_range() is a built-in function in C++ STL which returns a pair of iterators. The pair refers to the bounds of a range that includes all the elements in the container which have a key equivalent to k. Since the map container only contains unique key, hence the first iterator in the pai
2 min read
std::function in C++ The std::function() in C++ is a function wrapper class which can store and call any function or a callable object. In this article, we will learn about std::function in C++ and how to use it in different cases.Table of ContentWhat is std::function in C++?Example of std::functionMember Functions of s
5 min read
multimap equal_range() in C++ STL The multimap::equal_range() is a built-in function in C++ STL which returns an iterator of pairs. The pair refers to the bounds of a range that includes all the elements in the container which have a key equivalent to k. If there are no matches with key K, the range returned is of length 0 with both
2 min read
C# | Equals(String, String) Method In C#, Equals(String, String) is a String method. It is used to determine whether two String objects have the same value or not. Basically, it checks for equality. If both strings have the same value, it returns true otherwise returns false. This method is different from Compare and CompareTo method
2 min read
C# | Char.Equals() Method In C#, Char.Equals() is a System.Char struct method which is used to return a value by checking whether current instance is equal to a specified object or Char value. This method can be overloaded by passing different type of arguments to it. Char.Equals(Char) Method Char.Equals(Object) Method Char.
3 min read