C++ STL(6):最值

#include <iostream>
#include <ostream>
#include <algorithm>
#include <list>
#include <vector>

class ComplexTest
{
	public:
		ComplexTest(int r = 0, int i = 0):real(r),img(i){}

		friend bool less_than(const ComplexTest& c1, const ComplexTest& c2);
		friend bool greater_than(const ComplexTest& c1, const ComplexTest &c2);
		friend bool equal_to(const ComplexTest& c1, const ComplexTest &c2);

		friend std::ostream& operator<<(std::ostream& os,const ComplexTest & c)
		{
			os << "(" << c.real << "," << c.img << ")";
			return os;
		}
	public:
		int real{0};
		int img{0};
};

bool less_than(const ComplexTest& c1, const ComplexTest& c2)
{
	if(c1.real < c2.real)
		return true;
	else if(c1.real == c2.real && c1.img < c2.img)
		return true;
	else
		return false;
}

//最大值与最小值
int main()
{
	/************************************************************************/
	//min
	//max
	/************************************************************************/
	//min,max返回两个数中的小值或者大值
	//版本1:利用operator<
	//版本2:使用function object comp
	/*
		template<class Type>
		const Type& min(
			const Type& _Left, 
			const Type& _Right
		);
	*/
	/*
		template<class Type, class Pr>
		const Type& min(
			const Type& _Left, 
			const Type& _Right,
			BinaryPredicate _Comp
		);
	*/
	/*
		template<class Type>
		const Type& max(
			const Type& _Left, 
			const Type& _Right
		);
	*/
	/*
		template<class Type, class Pr>
		const Type& max(
			const Type& _Left, 
			const Type& _Right,
			BinaryPredicate _Comp
		);
	*/
	int a = 3, b = 9;
	int minNum = std::min(a, b);
	int maxNum = std::max(a, b);
	std::cout << "min: " << minNum << std::endl << "max: " << maxNum << std::endl;
	//min: 3
	//max: 9
	std::cout << std::endl;

	ComplexTest c1(3,4);
	ComplexTest c2(4,7);
	ComplexTest c3(3,7);

	ComplexTest c = std::min(c1, c2, less_than);
	std::cout << c1 << "和" << c2 << "的最小值为" << c << std::endl;
	//(3,4)和(4,7)的最小值为(3,4)

	c = std::max(c2, c3, less_than);
	std::cout << c2 << "和" << c3 << "的最大值为" << c << std::endl;
	//(4,7)和(3,7)的最大值为(4,7)
	std::cout << std::endl;
	 
	/************************************************************************/
	//min_element
	//max_element
	/************************************************************************/
	//寻找范围[first, last)中最小的元素并返回其iterator
	//版本1:利用operator<
	//版本2:使用function object comp
	/*
		template<class ForwardIterator>
		ForwardIterator min_element(
			ForwardIterator _First, 
			ForwardIterator _Last
		);
	*/
	/*
		template<class ForwardIterator, class BinaryPredicate>
		ForwardIterator min_element(
			ForwardIterator _First, 
			ForwardIterator _Last,
			BinaryPredicate _Comp
		);
	*/
	/*
		template<class ForwardIterator>
		ForwardIterator max_element(
			ForwardIterator _First, 
			ForwardIterator _Last
		);
	*/
	/*
		template<class ForwardIterator, class BinaryPredicate>
		ForwardIterator max_element(
			ForwardIterator _First, 
			ForwardIterator _Last, 
			BinaryPredicate _Comp
		);
	*/

	int A[] = {12,34,76,33,9};
	const int N = sizeof(A) / sizeof(int);
	int *pMin = std::min_element(A, A + N);
	std::cout << "A[] min: " << *pMin << std::endl;
	//A[] min: 9

	int *pMax = std::max_element(A, A + N);
	std::cout << "A[] max: " << *pMax << std::endl;
	//A[] max: 76

	std::cout << std::endl;
	ComplexTest c4(5,3);
	ComplexTest c5(1,8);
	std::vector<ComplexTest> cv;
	cv.push_back(c1);
	cv.push_back(c2);
	cv.push_back(c3);
	cv.push_back(c4);
	cv.push_back(c5);
	std::vector<ComplexTest>::iterator itMin = std::min_element(cv.begin(), cv.end(), less_than);
	std::cout << "cv[] min: " << *itMin << std::endl;
	//cv[] min: (1,8)

	std::vector<ComplexTest>::iterator itMax = std::max_element(cv.begin(), cv.end(), less_than);
	std::cout << "cv[] max: " << *itMax << std::endl;
	//cv[] max: (5,3)
	std::cout << std::endl;
	std::list<int> L;
	std::generate_n(front_inserter(L), 5, rand);
	std::list<int>::iterator ListItMin = std::min_element(L.begin(), L.end());
	std::cout << "L[] min: " << *ListItMin << std::endl;
	//L[] min: 41

	std::list<int>::iterator ListItMax = std::max_element(L.begin(), L.end());
	std::cout << "L[] max: " << *ListItMax << std::endl;
	//L[] max: 26500

	return 0;
}

====================打个广告,欢迎关注====================

QQ:412425870
csdn博客:
http://blog.youkuaiyun.com/caychen
码云:
https://gitee.com/caychen/
github:
https://github.com/caychen

点击群号或者扫描二维码即可加入QQ群:

328243383(1群)



点击群号或者扫描二维码即可加入QQ群:

180479701(2群)


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值