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
#include <bits/stdc++.h>
int main()
{
int v1[] = { 10, 20, 30, 40, 50 };
std::vector< int > vector_1 (v1, v1 + sizeof (v1) / sizeof ( int ) );
std::cout << "Vector contains : ";
for (unsigned int i = 0; i < vector_1.size(); i++)
std::cout << " " << vector_1[i];
std::cout << "\n";
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
#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 ) );
std::cout << "Vector contains : ";
for (unsigned int i = 0; i < vector_1.size(); i++)
std::cout << " " << vector_1[i];
std::cout << "\n";
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:
This article is contributed by Mohit Gupta_OMG 😀. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.