Equal STL Algorithm

Equal STL Algorithm

The equal() algorithm determines if two ranges are the same.Therange determined by first1 and last1 is tested against the sequence pointed to by start2. If the ranges are the same, true is returned. Otherwise, false is returned.
The second form allows you to specify a binary predicate that determines when two elements are equal.


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 first2, BinaryPredicate pred );

Test whether the elements in two ranges are equal

Implementation of equal() algorithm

#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

typedef int type;
const int size=10;

bool my_less (int item1, int item2 )
{
 return (item1 < item2);
}//end of my_less

int main ()
{
 const type list[size]= {5,6,7,8};

type const list0[size]= {5,6,7,8};
 vector<type> vec0(list0,list0+4);

type const list1[size]= {1,2,3,4};
 vector<type> vec1(list1,list1+4);

if (equal (vec0.begin(), vec0.end(), list))
 cout << "The contents are equal." << endl;
 else
 cout << "The contents differ." << endl;

if (equal (vec1.begin(), vec1.end(), list))
 cout << "The contents are equal." << endl;
 else
 cout << "The contents differ." << endl;

if (equal (vec1.begin(), vec1.end(), list,my_less ))
 cout << "The contents are less." << endl;
 else
 cout << "The contents differ." << endl;

return 0;
}// end of main

Program Output:

The contents are equal.
The contents differ.
The contents are less.
Proudly powered by WordPress   Premium Style Theme by www.gopiplus.com
%d bloggers like this: