mismatch

本文深入探讨了C++中mismatch函数的用法,包括其基本概念、参数、返回值和实际应用案例。
 
// mismatch.cpp -- 2011-10-01-22.37
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <algorithm>
#include <utility>
#include <functional>

using std ::vector ;
using std ::pair ;
using std ::greater ;

int _tmain(int argc, _TCHAR* argv[])
{
	int arr1[] = {1, 2, 3, 4, 5, 6, 7, 15, 9} ;
	vector<int> vec1(arr1, arr1 + sizeof arr1 / sizeof (int)) ;
	int arr2[] = {0, 2, 3, 4, 5, 6, 11, 55, 66, 77} ;
	vector<int> vec2(arr2, arr2 + sizeof arr2 / sizeof (int)) ;

	//	mismatch (beg1, end1, beg2) ;
	//	操作前:[beg,end)标示输入序列.[beg2,...)标示输入序列.
	//	操作后:确定指向两个输入序列第一个彼此不相等的元素的迭代器.
	//	返回值:返回一对迭代器.[beg,end)输入序列返回的迭代器作为first,[beg2,...)输入序列返回的迭代器作为second.
	//					如果存在对应位置不相等的元素,返回两个输入序列第一个不相等元素的迭代器对.
	//					否则,返回end1和beg2中偏移量为第一个输入序列长度的迭代器组成的迭代器对.
	//	备注:		[beg2,...)输入序列中的元素至少与[beg,end)输入序列一样多.否则将抛出异常.
	//					beg1和beg2的类型必须兼容,否则将无法通过编译.
	pair<vector<int> ::iterator, vector<int> ::iterator> resultPair =
		mismatch(vec1.begin(), vec1.end(), vec2.begin()) ;
	if (resultPair.first != vec1.end())
		std ::cout << *resultPair.first << std ::endl ;
	if (resultPair.second != vec2.end())
		std ::cout << *resultPair.second << std ::endl ;

	//	mismatch (beg1, end1, beg2, greater<int> ()) ;
	//	操作前:[beg,end)标示输入序列.[beg2,...)标示输入序列.greater<int> ()是二元函数对象.
	//	操作后:确定指向两个输入序列第一个彼此相等的元素的迭代器.
	//	返回值:返回一对迭代器.[beg,end)输入序列返回的迭代器作为first,[beg2,...)输入序列返回的迭代器作为second.
	//					如果存在对应位置不相等的元素,返回两个输入序列第一个相等元素的迭代器对.
	//					否则,返回end1和beg2中偏移量为第一个输入序列长度的迭代器组成的迭代器对.
	//	备注:		[beg2,...)输入序列中的元素至少与[beg,end)输入序列一样多.否则将抛出异常.
	//					beg1和beg2的类型必须兼容,否则将无法通过编译.
	resultPair = mismatch(vec1.begin(), vec1.end(), vec2.begin(), greater<int> ()) ;
	if (resultPair.first != vec1.end())
		std ::cout << *resultPair.first << std ::endl ;
	if (resultPair.second != vec2.end())
		std ::cout << *resultPair.second << std ::endl ;

	std ::cin.get() ;

	return 0 ;
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值