CPP - Test whether the elements in two ranges are equal or not using function
Hi, Everyone. I had just learned the C++ programming language from XYZ institute. Now, I have the good knowledge about the C++ programming language. But someone ask me about how can I test the ranges of the two element can be equal or not using the function in C++ programming. So, Can anyone knows about how to test the ranges of the two element can be equal or not using function and which function can be used. Reply Me!!
Test the elements in two ranges
Hello, I think you are looking for the function that can helps you to Test the elements in two ranges. I think the equal() function can be suitable for you that helps you to test the ranges between the two elements. The equal() function can be used for to compare the ranges of the two element. I hope this information can be sufficient for you to know about the equal() function.
Re: CPP - Test whether the elements in two ranges are equal or not using function
The equal() function can be used for to Test whether the elements in two ranges are equal or not, In C++ programming. The following can be the general syntax of the equal() function in C++ language :
Code:
template <class InptItratr1, class InptItratr2>
bool equal ( InptItratr1 fst1, InptItratr1 lst1,
InptItratr2 fst2 );
CPP - Test whether the elements in two ranges are equal or not using function
The following can be the parameters of the equal() function in the C++ language :
1. fst2 : This argument can be used to the starting position of the second sequence for forward iterator.
2. fst1, lst1 : This argument can be used to the final and starting positions of the first sequence for the forward iterators. This argument of the equal() can contains the all the elements that can not be pointed by the lst1 but the elements that can be pointed by the fst1.
Re: CPP - Test whether the elements in two ranges are equal or not using function
PROGRAM :
Code:
#include <vector>
#include <algorithm>
#include <iostream>
bool twice ( int elm1, int elm2 )
{
return elm1 * 2 == elm2;
}
int main( )
{
using namespace std;
vector <int> w1, w2, w3;
vector <int>::iterator Itr1, Itr2, Itr3;
int j;
for ( j = 0 ; j <= 5 ; j++ )
{
w1.pushback( 5 * j );
}
int jj;
for ( jj = 0 ; jj <= 5 ; jj++ )
{
w2.pushback( 5 * jj );
}
int jjj;
for ( jjj = 0 ; jjj <= 5 ; jjj++ )
{
w3.pushback( 10 * jjj );
}
cout << "w1 = ( " ;
for ( Itr1 = w1.begin( ) ; Itr1 != w1.end( ) ; Itr1++ )
cout << *Itr1 << " ";
cout << ")" << endl;
cout << "w2 = ( " ;
for ( Itr2 = w2.begin( ) ; Itr2 != w2.end( ) ; Itr2++ )
cout << *Itr2 << " ";
cout << ")" << endl;
cout << "w3 = ( " ;
for ( Itr3 = w3.begin( ) ; Itr3 != w3.end( ) ; Itr3++ )
cout << *Itr3 << " ";
cout << ")" << endl;
bool c;
c = equal( w1.begin( ), w1.end( ), w2.begin( ) );
if ( c )
cout << "The vectors w1 and w2 are equal under equality."
<< endl;
else
cout << "The vectors w1 and w2 are not equal under equality."
<< endl;
bool d;
d = equal( w1.begin( ), w1.end( ), w3.begin( ) );
if ( d )
cout << "The vectors w1 and w3 are equal under equality."
<< endl;
else
cout << "The vectors w1 and w3 are not equal under equality."
<< endl;
bool e;
e = equal( w1.begin( ), w1.end( ), w3.begin( ), twice );
if ( e )
cout << "The vectors w1 and w3 are equal under twice."
<< endl;
else
cout << "The vectors w1 and w3 are not equal under twice."
<< endl;
}