C++ STL算法系列5---equal() , mismatch()
equal和mismatch算法的功能是比较容器中的两个区间内的元素。这两个算法各有3个参数first1,last1和first2.如果对 于区间[first1,last1)内所有的first1+i,first1+i和first2所在位置处的元素都相等,则equal算法返回真,否则返 回假。mismatch算法的返回值是由两个迭代器first1+i和first2+i组成的一个pair,表示第1对不相等的元素的位置。如果没有找到 不相等的元素,则返回last1和first2+(last1-first1)。因此,语句equal(first1,last1,first2)和mismatch(first1,last1,first2).first==last1是等价的.
// Illustrating the generic equal and mismatch algorithms
#include <iostream>
#include <cassert>
#include <algorithm>
#include <string>
#include <list>
#include <deque>
#include <vector>
using namespace std; int main()
{
cout << "Illustrating the generic equal "
<< "and mismatch algorithms." << endl;
list<string> driver_list;
vector<string> vec;
deque<string> deq; driver_list.insert(driver_list.end(), "Clark");
driver_list.insert(driver_list.end(), "Rindt");
driver_list.insert(driver_list.end(), "Senna"); vec.insert(vec.end(), "Clark");
vec.insert(vec.end(), "Rindt");
vec.insert(vec.end(), "Senna");
vec.insert(vec.end(), "Berger"); deq.insert(deq.end(), "Clark");
deq.insert(deq.end(), "Berger"); // Show that driver_list and the first 3 elements of
// vec are equal in all corresponding positions:
assert (equal(driver_list.begin(), driver_list.end(),
vec.begin())); // Show that deq and the first 2 elements of driver_list
// are not equal in all corresponding positions:
assert (!equal(deq.begin(), deq.end(),
driver_list.begin())); // Find the corresponding positions in deq and driver_list
// at which unequal elements first occur:
pair<deque<string>::iterator, list<string>::iterator>
pair1 = mismatch(deq.begin(), deq.end(),
driver_list.begin()); if (pair1.first != deq.end())
cout << "First disagreement in deq and driver_list:\n "
<< *(pair1.first) << " and " << *(pair1.second)
<< endl;
return ;
}
equal算法类似于mismatch,equal算法也是逐一比较两个序列的元素是否相等,只是equal函数的返回值为bool值 true/false,不是返回迭代器值。它有如下两个原型,如果迭代器区间[first1,last1)和迭代器区间[first2, first2+(last1 - first1))上的元素相等(或者满足二元谓词判断条件binary_pred) ,返回true,否则返回false。
函数原型:
template<class InputIterator1, class InputIterator2>
bool equal(
InputIterator1 _First1,
InputIterator1 _Last1,
InputIterator2 _First2
);
template<class InputIterator1, class InputIterator2, class BinaryPredicate>
bool equal(
InputIterator1 _First1,
InputIterator1 _Last1,
InputIterator2 _First2,
BinaryPredicate _Comp
);
example:
利用二元谓词判断条件absEqual,判断出两个vector向量容器的元素均绝对值相等。
#include <algorithm>
#include <vector>
#include <iostream> using namespace std; bool absEqual(int a, int b)
{
return (a == abs(b) || b == abs(a)) ? true : false;
} int main()
{
vector<int> ivect1();
vector<int> ivect2(); for (vector<int>::size_type i = ; i < ivect1.size(); ++i)
{
ivect1[i] = i;
ivect2[i] = (-) * i;
}
if ( equal( ivect1.begin(), ivect1.end(), ivect2.begin(), absEqual ) )
{
cout << "ivect1 和 ivect2 元素的绝对值完全相等" << endl;
}
else
{
cout << "ivect1 和 ivect2 元素的绝对值不完全相等" << endl;
}
return ;
}
C++ STL算法系列5---equal() , mismatch()的更多相关文章
- C++ STL算法系列4---unique , unique_copy函数
一.unique函数 类属性算法unique的作用是从输入序列中“删除”所有相邻的重复元素. 该算法删除相邻的重复元素,然后重新排列输入范围内的元素,并且返回一个迭代器(容器的长度没变,只是元素顺序 ...
- C++ STL算法系列 unique
类属性算法unique的作用是从输入序列中“删除”所有相邻的重复元素. 该算法删除相邻的重复元素,然后重新排列输入范围内的元素,并且返回一个迭代器(容器的长度没变,只是元素顺序改变了),表示无重复的值 ...
- C++ STL算法系列1---unique , unique_copy函数
一.unique函数 类属性算法unique的作用是从输入序列中“删除”所有相邻的重复元素. 该算法删除相邻的重复元素,然后重新排列输入范围内的元素,并且返回一个迭代器(容器的长度没变,只是元素顺序 ...
- C++ STL算法系列2---find ,find_first_of , find_if , adjacent_find的使用
一.find运算 假设有一个int型的vector对象,名为vec,我们想知道其中是否包含某个特定值. 解决这个问题最简单的方法时使用标准库提供的find运算: 1 // value we'll lo ...
- C++ STL算法系列6---copy函数
现在我们来看看变易算法.所谓变易算法(Mutating algorithms)就是一组能够修改容器元素数据的模板函数,可进行序列数据的复制,变换等. 我们现在来看看第一个变易算法:元素复制算法copy ...
- C++ STL算法系列3---求和:accumulate
该算法在numeric头文件中定义. 假设vec是一个int型的vector对象,下面的代码: //sum the elements in vec starting the summation wit ...
- C++ STL算法系列1---count函数
一.count函数 algorithm头文件定义了一个count的函数,其功能类似于find.这个函数使用一对迭代器和一个值做参数,返回这个值出现次数的统计结果. 编写程序读取一系列int型数据,并将 ...
- C++ STL算法之:copy
C++ STL算法:copy 目录(?)[+] 前面十二个算法所展现的都属于非变易算法(Non-mutating algorithms)系列,现在我们来看看变易算法.所谓变易算法(Mutating a ...
- 实战c++中的vector系列--vector的遍历(stl算法、vector迭代器(不要在循环中推断不等于end())、operator[])
遍历一个vector容器有非常多种方法.使用起来也是仁者见仁. 通过索引遍历: for (i = 0; i<v.size(); i++) { cout << v[i] << ...
随机推荐
- jq 幻灯片插件制作
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...
- JavaWeb项目开发案例精粹-第6章报价管理系统-06po层
1. <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www ...
- QT静态编译
Qt静态编译(链接)和动态编译区别 Qt的静态编译译(其实应该叫链接,不是编译),将各模块编译成静态库,这样在编译自己写的Qt程序时,会将这些静态库编译(链接)到你的EXE文件中去的.Qt的动态编译, ...
- SQL Agent Job ->> 通过sys.sysprocesses的program_name字段来定位对应的Job
;WITH T AS ( SELECT 'SQLAgent - TSQL JobStep (Job 0x'+ ),)),) + ' : Step ' + )) + ')' filter, j.job_ ...
- [转]设置控件全局显示样式appearance proxy
转自:huifeidexin_1的专栏 appearance是apple在iOS5.0上加的一个协议,它让程序员可以很轻松地改变某控件的全局样式(背景) @selector(appearance) 支 ...
- 批处理 —— 每天生成一个以日期命名的文件(Win XP)
想达到这样一个效果:每天在某个目录下生成一个以日期命名的文件(如,0705.txt). 第一步,新建一个批处理文件 新建一个文件,比如[create_day_file.bat].编辑,输入以下内容 : ...
- java-基础练习题1
/** 题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,* 假如兔子都不死,问每个月的兔子总数为多少?* 1.程序分析: 兔子的规律为数列1, ...
- WebView中Js与Android本地函数的相互调用
介绍 随着Html5的普及,html在表现力上不一定比原生应用差,并且有很强的扩展兼容性,所以越来越多的应用是采用Html与Android原生混合开发模式实现. 既然要实现混合开发,那么Js与Andr ...
- UVa 1225 Digit Counting
题意:给出n,将前n个整数顺次写在一起,统计各个数字出现的次数. 用的最笨的办法--直接统计-- 后来发现网上的题解有先打表来做的 #include<iostream> #include& ...
- java基础:数据类型
一:基本数据类型 (1):整数类型 byte,short,int,long (2):浮点类型 float , double (3):布尔类型 boolean 注意: long 类型的变量后面要 ...