equal

C++ Library  
 

Header

<algorithm>
template<class InputIterator1, class InputIterator2>
bool equal(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2)

Compares two sequences to see if they match. equal compares the sequence [first1, last1) with a sequence of the same size starting at first2. It returns true if every corresponsing pair of elements match.

The non-predicate version uses operator== for comparison.

template<class InputIterator1, class InputIterator2, class BinaryPredicate>
bool equal(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, Predicate pr)

Compares two sequences to see if they match. equal compares the sequence [first1, last1) with a sequence of the same size starting at first2. It returns true if every corresponsing pair of elements match.

The predicate version uses the predicate function pr for comparison.


Sample

#pragma warning (disable :4786)

#include <algorithm>
#include <iostream>
#include <string>

struct Employee
{
	Employee(std::string name, long id)
	{
		m_name = name ;
		m_id = id ;
	}
	Employee() 
	{
	}

	friend std::ostream& operator<<(std::ostream& os, const Employee& re) ;
	std::string m_name ;
	long m_id ;

} ;

std::ostream& operator<<(std::ostream& os, const Employee& re)
{
	os << re.m_name << "\t" << re.m_id ;
	return os ;
}

bool compare(const Employee& e1, const Employee& e2)
{
	return ((e1.m_name == e2.m_name) && (e1.m_id == e1.m_id)) ;
}


int main()
{

	int set1[] = {10, 20, 35, 45, 50} ;
	int set2[] = {10, 20, 35, 45, 50} ;
	
	std::ostream_iterator<int> intostreamit(std::cout, ", ") ;

	bool result ;

	std::cout << "set1 = " ;
	std::copy(set1, set1+5, intostreamit) ;
	std::cout << std::endl ;

	std::cout << "set2 = " ;
	std::copy(set2, set2+5, intostreamit) ;
	std::cout << std::endl ;

	//is set1 == set2?
	//non-predicate version of equal
	result = std::equal(set1, set1+5, set2) ;
	

	if (result)
		std::cout << "set1 and set2 are equal" << std::endl ;
	else
		std::cout << "set1 and set2 are not equal" << std::endl ;

	Employee emp1[5], emp2[5] ;
	
	emp1[0] = Employee("Tim", 123456) ;
	emp1[1] = Employee("Jack", 123457) ;
	emp1[2] = Employee("Henry", 123458) ;
	emp1[3] = Employee("Lucy", 123459) ;
	emp1[4] = Employee("Liz", 123460) ;

	emp2[0] = Employee("Tim", 123456) ;
	emp2[1] = Employee("Jack", 123457) ;
	emp2[2] = Employee("Henry", 123458) ;
	emp2[3] = Employee("Lucy", 123459) ;
	emp2[4] = Employee("Liz", 123460) ;
	
	int i ;

	std::cout << "\nemp1 = " << std::endl;
	for(i = 0; i < 5; i++)
		std::cout << emp1[i] << std::endl ;
	std::cout << std::endl ;

	std::cout << "emp2 = " << std::endl;
	for(i = 0; i < 5; i++)
		std::cout << emp2[i] << std::endl ;
	std::cout << std::endl ;

	//is emp1 == emp2?
	//predicate version of equal
	result = std::equal(emp1, emp1+5, emp2, compare) ;

	if (result)
		std::cout << "emp1 and emp2 are equal" << std::endl ;
	else
		std::cout << "emp1 and emp2 are not equal" << std::endl ;

	return 0 ;
}

Program Output

set1 = 10, 20, 35, 45, 50,
set2 = 10, 20, 35, 45, 50,
set1 and set2 are equal

emp1 =
Tim     123456
Jack    123457
Henry   123458
Lucy    123459
Liz     123460

emp2 =
Tim     123456
Jack    123457
Henry   123458
Lucy    123459
Liz     123460

emp1 and emp2 are equal

© 1997 Microsoft Corporation. All rights reserved. Terms of Use.