std::mismatch 用法

描述:
       寻找两个容器出现不同元素的首个位置。
       用 operator== 比较元素。

定义:

template< class InputIt1, class InputIt2 >
std::pair<InputIt1,InputIt2>
    mismatch( InputIt1 first1, InputIt1 last1,
              InputIt2 first2 );

template< class InputIt1, class InputIt2 >
constexpr std::pair<InputIt1,InputIt2>
              mismatch( InputIt1 first1, InputIt1 last1,
                        InputIt2 first2 );
template< class InputIt1, class InputIt2, class BinaryPredicate >
std::pair<InputIt1,InputIt2>
    mismatch( InputIt1 first1, InputIt1 last1,
              InputIt2 first2,
              BinaryPredicate p );

template< class InputIt1, class InputIt2, class BinaryPredicate >
constexpr std::pair<InputIt1,InputIt2>
              mismatch( InputIt1 first1, InputIt1 last1,
                        InputIt2 first2,
                        BinaryPredicate p );
template< class InputIt1, class InputIt2 >
std::pair<InputIt1,InputIt2>
    mismatch( InputIt1 first1, InputIt1 last1,
              InputIt2 first2, InputIt2 last2 );

template< class InputIt1, class InputIt2 >
constexpr std::pair<InputIt1,InputIt2>
              mismatch( InputIt1 first1, InputIt1 last1,
                        InputIt2 first2, InputIt2 last2 );
template< class InputIt1, class InputIt2, class BinaryPredicate >
std::pair<InputIt1,InputIt2>
    mismatch( InputIt1 first1, InputIt1 last1,
              InputIt2 first2, InputIt2 last2,
              BinaryPredicate p );

template< class InputIt1, class InputIt2, class BinaryPredicate >
constexpr std::pair<InputIt1,InputIt2>
              mismatch( InputIt1 first1, InputIt1 last1,
                        InputIt2 first2, InputIt2 last2,
                        BinaryPredicate p );

可能的实现:

template<class InputIt1, class InputIt2>
std::pair<InputIt1, InputIt2>
    mismatch(InputIt1 first1, InputIt1 last1, InputIt2 first2)
{
    while (first1 != last1 && *first1 == *first2) {
        ++first1, ++first2;
    }
    return std::make_pair(first1, first2);
}
template<class InputIt1, class InputIt2, class BinaryPredicate>
std::pair<InputIt1, InputIt2>
    mismatch(InputIt1 first1, InputIt1 last1, InputIt2 first2, BinaryPredicate p)
{
    while (first1 != last1 && p(*first1, *first2)) {
        ++first1, ++first2;
    }
    return std::make_pair(first1, first2);
}
template<class InputIt1, class InputIt2>
std::pair<InputIt1, InputIt2>
    mismatch(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2)
{
    while (first1 != last1 && first2 != last2 && *first1 == *first2) {
        ++first1, ++first2;
    }
    return std::make_pair(first1, first2);
}
template<class InputIt1, class InputIt2, class BinaryPredicate>
std::pair<InputIt1, InputIt2>
    mismatch(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, BinaryPredicate p)
{
    while (first1 != last1 && first2 != last2 && p(*first1, *first2)) {
        ++first1, ++first2;
    }
    return std::make_pair(first1, first2);
}

参数:
       first1, last1 - 第一元素范围
       first2, last2 - 第二元素范围
       p - 若元素应被当做相等则返回 ​true 的二元谓词。

返回值:
       返回指向首二个不相等元素的迭代器的 std::pair 。

示例:

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

int main()
{
	std::vector<int> vecInt1 = { 1,2,3,4,5,6 };
	std::vector<int> vecInt2 = { 1,2,3,5,6,7};
	//容器2的元素数量要大于等于容器1
	auto pair = std::mismatch(vecInt1.begin(), vecInt1.end(), vecInt2.begin());
	
	int nIndex1 = std::distance(std::begin(vecInt1), pair.first);
	int nValue1 = *pair.first;
	std::cout << "容器1首个不同元素索引:" << nIndex1 << ",值:" << nValue1 << std::endl;
	//容器1首个不同元素索引:3,值:4

	int nIndex2 = std::distance(std::begin(vecInt2), pair.second);
	int nValue2 = *pair.second;
	std::cout << "容器2首个不同元素索引:" << nIndex2 << ",值:" << nValue2 << std::endl;
	//容器2首个不同元素索引:3,值:5

	return 0;
}
#include <iostream>
#include <algorithm>
#include <vector>

struct SData
{
	int m_nData;
	double m_dData;

	SData(int nData, double dData)
		:m_nData(nData),m_dData(dData)
	{
	}
};

int main()
{
	std::vector<SData> v1;
	for (int i = 0; i < 5; i++)
	{
		v1.push_back(SData(i,i));
	}
	std::vector<SData> v2;
	for (int i = 1; i < 6; i++)
	{
		v2.push_back(SData(i, i));
	}
	
	//容器2的元素数量要大于等于容器1
	//使用lambda表达式指定条件(一般用于自定义元素)
	auto pair = std::mismatch(v1.begin(), v1.end(), v2.begin(), [](const SData& data1,const SData& data2) 
	{
		return data1.m_nData == data2.m_nData;
	});
	
	int nIndex1 = std::distance(std::begin(v1), pair.first);
	SData value1 = *pair.first;
	std::cout << "容器1首个不同元素索引:" << nIndex1 << ",值:" << value1.m_nData << std::endl;
	//容器1首个不同元素索引:0,值:0

	int nIndex2 = std::distance(std::begin(v2), pair.second);
	SData value2 = *pair.second;
	std::cout << "容器2首个不同元素索引:" << nIndex2 << ",值:" << value2.m_nData << std::endl;
	//容器2首个不同元素索引:0,值:1

	return 0;
}
#include <iostream>
#include <algorithm>
#include <vector>

int main()
{
	std::vector<int> vecInt1 = { 1,2,3,4,5,6 };
	std::vector<int> vecInt2 = { 1,2,3,5,6 };
	auto pair = std::mismatch(vecInt1.begin(), vecInt1.end(), vecInt2.begin(), vecInt2.end());
	
	int nIndex1 = std::distance(std::begin(vecInt1), pair.first);
	int nValue1 = *pair.first;
	std::cout << "容器1首个不同元素索引:" << nIndex1 << ",值:" << nValue1 << std::endl;
	//容器1首个不同元素索引:3,值:4

	int nIndex2 = std::distance(std::begin(vecInt2), pair.second);
	int nValue2 = *pair.second;
	std::cout << "容器2首个不同元素索引:" << nIndex2 << ",值:" << nValue2 << std::endl;
	//容器2首个不同元素索引:3,值:5

	return 0;
}
#include <iostream>
#include <algorithm>
#include <vector>

struct SData
{
	int m_nData;
	double m_dData;

	SData(int nData, double dData)
		:m_nData(nData),m_dData(dData)
	{
	}
};

int main()
{
	std::vector<SData> v1;
	for (int i = 0; i < 5; i++)
	{
		v1.push_back(SData(i,i));
	}
	std::vector<SData> v2;
	for (int i = 1; i < 5; i++)
	{
		v2.push_back(SData(i, i));
	}

	//使用lambda表达式指定条件(一般用于自定义元素)
	auto pair = std::mismatch(v1.begin(), v1.end(), v2.begin(),v2.end(), [](const SData& data1,const SData& data2) 
	{
		return data1.m_nData == data2.m_nData;
	});
	
	int nIndex1 = std::distance(std::begin(v1), pair.first);
	SData value1 = *pair.first;
	std::cout << "容器1首个不同元素索引:" << nIndex1 << ",值:" << value1.m_nData << std::endl;
	//容器1首个不同元素索引:0,值:0

	int nIndex2 = std::distance(std::begin(v2), pair.second);
	SData value2 = *pair.second;
	std::cout << "容器2首个不同元素索引:" << nIndex2 << ",值:" << value2.m_nData << std::endl;
	//容器2首个不同元素索引:0,值:1
	
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值