min

C++ Library  
 

Header

<algorithm>
template<class T>
const T& _cpp_min(const T& x, const T& y)

Returns the minimum of the two items. _cpp_min returns a reference to the smaller of the x and y.

The non-predicate version uses operator< for comparison.

template<class T, BinaryPredicate>
const T& _cpp_min(const T& x, const T& y, BinaryPredicate pr)

Returns the minimum of the two items. _cpp_min returns a reference to the smaller of the x and y.

The predicate version uses the predicate function pr for comparison.

Microsoft Specific

NOTE: To avoid conflicts with max and min in WINDEF.H, use _MAX and _MIN. These macros evaluate to _cpp_max and _cpp_min

End Microsoft Specific


Samples

#pragma warning (disable: 4786)

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

struct PhoneBook
{
	PhoneBook(std::string s, long num)
	{
		name = s ;
		number = num ;
	}
	friend std::ostream& operator<<(std::ostream&, const PhoneBook&) ;
	long number ;
	std::string name ;
} ;

std::ostream& operator<<(std::ostream& os, const PhoneBook& rpb)
{
	os << rpb.name << "\t" << rpb.number << std::endl ;
	return os ;
}

bool compare(const PhoneBook& r1, const PhoneBook& r2)
{
	return (r2.name > r1.name) ;
}


int main()
{
	//non-predicate version of _MAX
	std::cout << "_MAX(10, 30) = " << std::_MAX(10, 30)
		<< std::endl ;

	//non-predicate version of _MIN
	std::cout << "_MIN(10, 30) = " << std::_MIN(10, 30)
		<< std::endl ;

	
	PhoneBook s1("Tom", 8612345) ;
	PhoneBook s2("Jack", 6782345) ;
	
	//predicate version of _MAX
	std::cout << "_MAX(s1, s2, max_PhoneBook) = " << std::_MAX(s1, s2, compare)
		<< std::endl ;

		//predicate version of _MAX
	std::cout << "_MIN(s1, s2, min_PhoneBook) = " << std::_MIN(s1, s2, compare)
		<< std::endl ;

	return 0 ;
}

Program Output

_MAX(10, 30) = 30
_MIN(10, 30) = 10
_MAX(s1, s2, max_PhoneBook) = Tom       8612345

_MIN(s1, s2, min_PhoneBook) = Jack      6782345


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