栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++ STL算法mismatch(14)

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++ STL算法mismatch(14)

函数原型
//不带谓词
template
_NODISCARD inline pair<_InIt1, _InIt2> mismatch(const _InIt1 _First1, const _InIt1 _Last1, const _InIt2 _First2)

template
_NODISCARD inline pair<_InIt1, _InIt2> mismatch(const _InIt1 _First1, const _InIt1 _Last1, const _InIt2 _First2)

//带谓词
template
_NODISCARD inline pair<_InIt1, _InIt2> mismatch(_InIt1 _First1, const _InIt1 _Last1, _InIt2 _First2, _Pr _Pred)

template
_NODISCARD inline pair<_InIt1, _InIt2> mismatch(_InIt1 _First1, const _InIt1 _Last1, _InIt2 _First2, _Pr _Pred)

 如下两个序列为例:

序列 1:1, 2, 3, 4, 5, 1, 2, 3, 4, 5

序列 2:1, 2, 3, 9, 10

用于比较两个序列是否匹配,如果不匹配,返回不匹配的迭代器。返回上面序列中序列1中的4和序列中的9组成pair的迭代器。

参数
first1、last1:都为正向迭代器,其组合 [first1, last1) 用于指定查找范围(即序列 1);

first2、last2:都为正向迭代器,其组合 [first2, last2) 用于指定要查找的序列(即序列 2);

pred:自定义查找规则。该规则实际上是一个包含 2 个参数且返回值类型为 bool 的函数(第一个参数接收 [first1, last1) 范围内的元素,第二个参数接收 [first2, last2) 范围内的元素)。函数定义的形式可以是普通函数,也可以是函数对象。

 不带谓词
#include 
#include 
#include 
#include 
int main()
{	

	std::string a{ "abcdefg" };
	std::string b{ "abcefgf" };
	using str = std::pair ;
	str it = std::mismatch(std::begin(a), std::end(a), std::begin(b));
	if (std::get<0>(it) != std::end(a))
	{
		std::cout << "the first not matching elements: aa = " << *it.first << "; bb = " << *it.second << std::endl;
	}
	std::cout << std::endl;

	std::vector aa{ 1, 2, 3, 4, 5, 1, 2, 3, 4, 5 };
	std::vector bb{ 1, 2, 3, 9, 10 };

	using type = std::pair::iterator, std::vector::iterator>;
	type it1 = std::mismatch(std::begin(aa), std::end(aa), std::begin(bb), std::end(bb));

	if (std::get<0>(it1) != std::end(aa))
	{
		std::cout << "the first not matching  elements: aa = " << *it1.first << "; bb = " << *it1.second << std::endl;
	}


	return -1;
}

//输出
the first not matching elements: aa = d; bb = e

the first not matching  elements: aa = 4; bb = 9
带谓词
 函数
#include 
#include 
#include 
#include 

bool cha(char a, char b)
{
	std::cout << "a = " << a << "; b = " << b << std::endl;
	return b > a;
}

bool greater(int a, int b)
{
	std::cout << "a = " << a << "; b = " << b << std::endl;
	return b > a;
}

int main()
{	

	std::string a{ "abcdefg" };
	std::string b{ "bcefggf" };
	using str = std::pair ;
	str it = std::mismatch(std::begin(a), std::end(a), std::begin(b), cha);
	if (std::get<0>(it) != std::end(a))
	{
		std::cout << "the first not matching elements: aa = " << *it.first << "; bb = " << *it.second << std::endl;
	}
	std::cout << std::endl;

	std::vector aa{ 1, 2, 3, 10, 5 };
	std::vector bb{ 2, 3, 9, 3, 1};
	std::function f1 = greater;
	using type = std::pair::iterator, std::vector::iterator>;
	type it1 = std::mismatch(std::begin(aa), std::end(aa), std::begin(bb), std::end(bb), f1);

	if (std::get<0>(it1) != std::end(aa))
	{
		std::cout << "the first not matching  elements: aa = " << *it1.first << "; bb = " << *it1.second << std::endl;
	}

	return -1;
}

//输出
a = a; b = b
a = b; b = c
a = c; b = e
a = d; b = f
a = e; b = g
a = f; b = g
a = g; b = f
the first not matching elements: aa = g; bb = f

a = 1; b = 2
a = 2; b = 3
a = 3; b = 9
a = 10; b = 3
the first not matching  elements: aa = 10; bb = 3

备注: 

bool cha(char a, char b)
{
	std::cout << "a = " << a << "; b = " << b << std::endl;
	return b > a;
}

当b中的元素都大于a时,迭代到最后会报错。(访问越界)
std::string a{ "abcdefg" };
std::string b{ "bcdefg" }; 



//当bb中的元素都大于aa时,迭代到最后会报错。同理也报错
std::vector aa{ 5, 7, 3, 10, 5 };
std::vector bb{ 21, 22};

 仿函数
#include 
#include 
#include 
#include 

class Greater
{
public:
	bool operator()(char a, char b)
	{
		std::cout << "a = " << a << "; b = " << b << std::endl;
		return b == a;
	}
};

class greater
{
public:
	bool operator()(int a, int b)
	{
		std::cout << "a = " << a << "; b = " << b << std::endl;
		return b == a;
	}
};


int main()
{	
	std::string a{ "abcdefg" };
	std::string b{ "bcdaefg" };
	using str = std::pair ;
	str it = std::mismatch(std::begin(a), std::end(a), std::begin(b), Greater());
	if (std::get<0>(it) != std::end(a))
	{
		std::cout << "the first not matching elements at " << std::distance(std::begin(a), std::get<0>(it)) << ", a = "
			<< *it.first << "; " << std::distance(std::begin(b), std::get<1>(it)) << " b = " << *it.second << std::endl;
	}
	std::cout << std::endl;

	std::vector aa{ 2, 1, 3, 10, 5 };
	std::vector bb{ 5, 2, 20, 4};
	using type = std::pair::iterator, std::vector::iterator>;
	type it1 = std::mismatch(std::begin(aa), std::end(aa), std::begin(bb), std::end(bb), greater());

	if (std::get<0>(it1) != std::end(aa))
	{
		std::cout << "the first not matching  elements: aa = " << *it1.first << "; bb = " << *it1.second << std::endl;
	}

	return -1;
}

//输出
a = a; b = b
the first not matching elements at 0, a = a; 0 b = b

a = 2; b = 5
the first not matching  elements: aa = 2; bb = 5
bind
#include 
#include 
#include 
#include 

bool cha(char a, char b)
{
	std::cout << "a = " << a << "; b = " << b << std::endl;
	return b > a;
}

class equal
{
public:
	bool operator()(int a, int b)
	{
		std::cout << "a = " << a << "; b = " << b << std::endl;
		return b == a;
	}
};

int main()
{	
	std::string a{ "abcdefg" };
	std::string b{ "bcdaefg" };
	auto f1 = std::bind(cha, std::placeholders::_1, std::placeholders::_2);
	using str = std::pair ;
	str it = std::mismatch(std::begin(a), std::end(a), std::begin(b), f1);
	if (std::get<0>(it) != std::end(a))
	{
		std::cout << "the first not matching elements at " << std::distance(std::begin(a), std::get<0>(it)) << ", a = "
			<< *it.first << "; " << std::distance(std::begin(b), std::get<1>(it)) << " b = " << *it.second << std::endl;
	}
	std::cout << std::endl;

	std::vector aa{ 2, 1, 3, 10, 5 };
	std::vector bb{ 5, 2, 20, 4};
	auto f2 = std::bind(greater(), std::placeholders::_1, std::placeholders::_2);
	using type = std::pair::iterator, std::vector::iterator>;
	type it1 = std::mismatch(std::begin(aa), std::end(aa), std::begin(bb), std::end(bb), f2);

	if (std::get<0>(it1) != std::end(aa))
	{
		std::cout << "the first not matching  elements: aa = " << *it1.first << "; bb = " << *it1.second << std::endl;
	}

	return -1;
}

//输出
a = a; b = b
a = b; b = c
a = c; b = d
a = d; b = a
the first not matching elements at 3, a = d; 3 b = a

a = 2; b = 5
the first not matching  elements: aa = 2; bb = 5
lambda
#include 
#include 
#include 
#include 

int main()
{	
	std::string a{ "abcdefg" };
	std::string b{ "bcdaefg" };
	using str = std::pair ;
	str it = std::mismatch(std::begin(a), std::end(a), std::begin(b), [](int a, int b) {
		std::cout << "a = " << a << "; b = " << b << std::endl;
		return b > a; });
	if (std::get<0>(it) != std::end(a))
	{
		std::cout << "the first not matching elements at " << std::distance(std::begin(a), std::get<0>(it)) << ", a = "
			<< *it.first << "; " << std::distance(std::begin(b), std::get<1>(it)) << " b = " << *it.second << std::endl;
	}
	std::cout << std::endl;

	std::vector aa{ 2, 1, 3, 10, 5 };
	std::vector bb{ 5, 2, 20, 4};
	using type = std::pair::iterator, std::vector::iterator>;
	type it1 = std::mismatch(std::begin(aa), std::end(aa), std::begin(bb), std::end(bb), [](int a, int b) {
		std::cout << "a = " << a << "; b = " << b << std::endl;
		return a == b;
	});

	if (std::get<0>(it1) != std::end(aa))
	{
		std::cout << "the first not matching  elements: aa = " << *it1.first << "; bb = " << *it1.second << std::endl;
	}

	return -1;
}

//输出
a = 97; b = 98
a = 98; b = 99
a = 99; b = 100
a = 100; b = 97
the first not matching elements at 3, a = d; 3 b = a

a = 2; b = 5
the first not matching  elements: aa = 2; bb = 5

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/656975.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号