// 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 ;
}
mismatch
最新推荐文章于 2024-09-20 08:30:00 发布
本文深入探讨了C++中mismatch函数的用法,包括其基本概念、参数、返回值和实际应用案例。
563

被折叠的 条评论
为什么被折叠?



