C++ STL(5):范围Range的比较

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

bool lt_nocase(char c1, char c2)
{
	return toupper(c1) < toupper(c2);
}

bool equal_nocase(char c1, char c2)
{
	return toupper(c1) == toupper(c2);
}

bool equal_nosign(int x1, int x2)
{
	return abs(x1) == abs(x2);
}

//范围Ranges比较
int main()
{
	/************************************************************************/
	//equal
	/************************************************************************/
	//equal:将范围[first1,last1)和范围[first2,first2+(last2-last1))进行一一比较,如果相等,函数返回true,否则返回false
	//版本1:利用operator==
	//版本2:使用function object binary_pred
	/*
		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 _Comp
		);
	*/
	//测试版本1:
	int a[] = {3, 1, 4, 1, 5, 9, 3};
	int b[] = {3, 1, 4, 1, 5, 8, 3};
	const int N = sizeof a / sizeof a[0];

	if(std::equal(a, a + N, b))
		std::cout << "Equal" << std::endl;
	else
		std::cout << "Not equal" << std::endl;//Not equal

	//测试版本2:
	const char *s1 = "HelLo";
	const char *s2 = "hELlo wOrlD";
	const int LEN = strlen(s1);

	if(std::equal(s1, s1 + LEN, s2, equal_nocase))
		std::cout << "Equal" << std::endl;//Equal
	else
		std::cout << "Not equal" << std::endl;

	/************************************************************************/
	//mismatch
	/************************************************************************/
	//mismatch返回范围[first1,last1)与范围[first2,first2+(last1-first1))之间第一个元素值不同的位置
	//函数返回一个pair,包含两个iterator,分别表示两个范围中第一个元素值不匹配的迭代器位置
	//版本1:利用operator==
	//版本2:使用function object binary_pred
	/*
		template<class InputIterator1, class InputIterator2>
		pair<InputIterator1, InputIterator2> mismatch(
			InputIterator1 _First1, 
			InputIterator1 _Last1,
			InputIterator2 _First2
		);
	*/
	/*
		template<class InputIterator1, class InputIterator2, class BinaryPredicate>
		pair<InputIterator1, InputIterator2> mismatch(
			InputIterator1 _First1, 
			InputIterator1 _Last1,
			InputIterator2 _First2
			BinaryPredicate _Comp
		);
	*/

	std::pair<int*, int*> res = std::mismatch(a, a + N, b);
	if(res.first == a + N)
		std::cout << "The ranges are same." << std::endl;
	else
	{
		//First mismatch is in position 5
		//Value:9,8
		std::cout << "First mismatch is in position " << res.first - a << std::endl
			<< "Value:" << *(res.first) << "," << *(res.second) << std::endl;
	}

	const char *ss1 = "HelLo";
	const char *ss2 = "hELlo";
	std::pair<const char*, const char*> rs = std::mismatch(ss1, ss1 + strlen(ss1), ss2, equal_nocase);
	if(rs.first == ss1 + strlen(ss1))
	{
		//The ranges are same.
		std::cout << "The ranges are same." << std::endl;
	}
	else
	{
		std::cout << "These two strings differ starting at character " << rs.first - ss1 << std::endl
			<< "Part of s1: " << rs.first << std::endl
			<< "Part of s2: " << rs.second << std::endl;
	}

	std::vector<int> iv1{1,2,3,4,5};
	std::vector<int> iv2{1,2,-3,4,5};
	typedef std::vector<int>::iterator intIt;
	std::pair<intIt, intIt> rr = std::mismatch(iv1.begin(), iv1.end(), iv2.begin(), iv2.end(), equal_nosign);
	if(rr.first == iv1.end())
	{
		//Two ranges are same.
		std::cout << "Two ranges are same." << std::endl;
	}
	else
	{
		//...
	}


	/************************************************************************/
	//lexicographical_compare
	/************************************************************************/
	//lexicographical_compare以字典排序法进行比较。函数类似字符串比较函数strcmp
	//如果*first1小于*first2,则[first1,last1)小于[first2,last2),
	//如果*first1大于*first2,则[first1,last1)大于[first2,last2),
	//如果*first1等于*first2,则依次比较下一个元素,依次类推
	//版本1:利用operator<
	//版本2:使用function object comp
	/*
		template<class InputIterator1, class InputIterator2>
		bool lexicographical_compare(
			InputIterator1 _First1,
			InputIterator1 _Last1,
			InputIterator2 _First2,
			InputIterator2 _Last2
		);
	*/
	/*
		template<class InputIterator1, class InputIterator2, class BinaryPredicate>
		bool lexicographical_compare(
			InputIterator1 _First1,
			InputIterator1 _Last1, 
			InputIterator2 _First2, 
			InputIterator2 _Last2,
			BinaryPredicate _Comp
		);
	*/
	int A1[] = {3,1,4,1,5,9,3};
	int A2[] = {3,1,4,2,8,5,7};
	int A3[] = {1,2,3,4};
	int A4[] = {1,2,3,4,5};
	const int N1 = sizeof A1 / sizeof(int);
	const int N2 = sizeof A2 / sizeof(int);
	const int N3 = sizeof A3 / sizeof(int);
	const int N4 = sizeof A4 / sizeof(int);
	bool bFlag12 = std::lexicographical_compare(A1, A1 + N1, A2, A2 + N2);
	bool bFlag34 = std::lexicographical_compare(A3, A3 + N3, A4, A4 + N4);
	std::cout << "A1[] < A2[]: " << (bFlag12 ? "true" : "false") << std::endl;//true
	std::cout << "A3[] < A4[]: " << (bFlag34 ? "true" : "false") << std::endl;//true

	const char *ps1 = "abc";
	const char *ps2 = "ABC";
	const char *ps3 = "abcDEF";
	const int NS1 = 3,NS2 = 3,NS3 = 6;

	//abc < ABC: false
	std::cout << ps1 << " < " << ps2 << ": " << 
		(std::lexicographical_compare(ps1, ps1 + NS1, ps2, ps2 + NS2, lt_nocase) ? "true" : "false") << std::endl;

	//ABC < abcDEF: true
	std::cout << ps2 << " < " << ps3 << ": " << 
		(std::lexicographical_compare(ps2, ps2 + NS2, ps3, ps3 + NS3, lt_nocase) ? "true" : "false") << std::endl;

	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、付费专栏及课程。

余额充值