简单的程序诠释C++ STL算法系列之八:mismatch
来源:互联网 发布:数据质量问题库设计 编辑:程序博客网 时间:2024/06/09 08:22
C++STL的非变易算法(Non-mutating algorithms)是一组不破坏操作数据的模板函数,用来对序列数据进行逐个处理、元素查找、子序列搜索、统计和匹配。
mismatch算法是比较两个序列,找出首个不匹配元素的位置。它有如下两个函数原型,找出迭代器区间[first1, last1) 上第一个元素 *i , 它和迭代器区间[first2, first2 + (last1 - first1))上的元素* (first2 + (i - first1))不相等(或者不满足二元谓词binary_pred条件)。通过匹配对象pair返回这两个元素的迭代器,指示不匹配元素位置。
函数原型:
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 );
示例代码:
/******************************************************************* * Copyright (C) Jerry Jiang * File Name : mismatch.cpp * Author : Jerry Jiang * Create Time : 2011-10-9 21:16:53 * Mail : jbiaojerry@gmail.com * Blog : http://blog.csdn.net/jerryjbiao * Description : 简单的程序诠释C++ STL算法系列之八 * 非变易算法 : 元素不匹配查找mismatch ******************************************************************/#include <algorithm>#include <vector>#include <iostream>using namespace std;bool strEqual(const char* s1, const char* s2){return strcmp(s1, s2) == 0 ? true : false;}typedef vector<int>::iterator ivecIter;int main(){vector<int> ivec1, ivec2;ivec1.push_back(2);ivec1.push_back(0);ivec1.push_back(1);ivec1.push_back(4);ivec2.push_back(2);ivec2.push_back(0);ivec2.push_back(1);ivec2.push_back(7);pair<ivecIter, ivecIter> retCode;retCode = mismatch(ivec1.begin(), ivec1.end(), ivec2.begin());if (retCode.first == ivec1.end() && retCode.second == ivec2.end() /* ivec2.begin() */){cout << "ivec1 和 ivec2完全相同" << endl;} else{cout << "ivec1 和 ivec2 不相同,不匹配的元素为:\n" << *retCode.first << endl << *retCode.second << endl;} char* str1[] = {"appple", "pear", "watermelon", "banana", "grape"};char* str2[] = {"appple", "pears", "watermelons", "banana", "grape"};pair<char**, char**> retCode2 = mismatch(str1, str1+5, str2, strEqual);if (retCode2.first == str1+5 && retCode2.second == str2+5){cout << "str1 和 str2 完全相同" << endl;} else{cout << "str1 和 str2 不相同,不匹配的字符串为:" << endl << str1[retCode2.first - str1] << endl << str2[retCode2.second - str2] << endl;}return 0;}
*******************************************************************************************************************************
C++经典书目索引及资源下载:http://blog.csdn.net/jerryjbiao/article/details/7358796
********************************************************************************************************************************
- 简单的程序诠释C++ STL算法系列之八:mismatch
- 简单的程序诠释C++ STL算法系列之一:for_each
- 简单的程序诠释C++ STL算法系列之一:for_each
- 简单的程序诠释C++ STL算法系列: find & find_if
- 简单的程序诠释C++ STL算法系列之一:for_each
- 简单的程序诠释C++ STL算法系列之二:find
- 简单的程序诠释C++ STL算法系列之三:find_if
- 简单的程序诠释C++ STL算法系列之四:adjacent_find
- 简单的程序诠释C++ STL算法系列之五:find_first_of
- 简单的程序诠释C++ STL算法系列之六:count
- 简单的程序诠释C++ STL算法系列之七:count_if
- 简单的程序诠释C++ STL算法系列之九:equal
- 简单的程序诠释C++ STL算法系列之十:search
- 简单的程序诠释C++ STL算法系列之十一:search_n
- 简单的程序诠释C++ STL算法系列之十二:find_end
- 简单的程序诠释C++ STL算法系列之十三:copy
- 简单的程序诠释C++ STL算法系列之十四:copy_backward
- 简单的程序诠释C++ STL算法系列之十五:swap
- log 相关
- POJ - 1364 巧妙构图的差分约束
- boot loader,Linux 内核,文件系统之间的关系
- EDK的IP设计中调用ISE的IP
- android控件学习——GridView学习
- 简单的程序诠释C++ STL算法系列之八:mismatch
- Spket安装方法和如何使用
- c/c++变量在内存中的分布
- Tiles使用总结
- zsacm第一期网络练习解题报告
- 使用Lucene 3.3.0的结构遍历TokenStream的内容.
- 一个牛人的播客
- linux串口通信mark问题
- 安装支持magento的lnmp