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()的更多相关文章

  1. C++ STL算法系列4---unique , unique_copy函数

     一.unique函数 类属性算法unique的作用是从输入序列中“删除”所有相邻的重复元素. 该算法删除相邻的重复元素,然后重新排列输入范围内的元素,并且返回一个迭代器(容器的长度没变,只是元素顺序 ...

  2. C++ STL算法系列 unique

    类属性算法unique的作用是从输入序列中“删除”所有相邻的重复元素. 该算法删除相邻的重复元素,然后重新排列输入范围内的元素,并且返回一个迭代器(容器的长度没变,只是元素顺序改变了),表示无重复的值 ...

  3. C++ STL算法系列1---unique , unique_copy函数

     一.unique函数 类属性算法unique的作用是从输入序列中“删除”所有相邻的重复元素. 该算法删除相邻的重复元素,然后重新排列输入范围内的元素,并且返回一个迭代器(容器的长度没变,只是元素顺序 ...

  4. C++ STL算法系列2---find ,find_first_of , find_if , adjacent_find的使用

    一.find运算 假设有一个int型的vector对象,名为vec,我们想知道其中是否包含某个特定值. 解决这个问题最简单的方法时使用标准库提供的find运算: 1 // value we'll lo ...

  5. C++ STL算法系列6---copy函数

    现在我们来看看变易算法.所谓变易算法(Mutating algorithms)就是一组能够修改容器元素数据的模板函数,可进行序列数据的复制,变换等. 我们现在来看看第一个变易算法:元素复制算法copy ...

  6. C++ STL算法系列3---求和:accumulate

    该算法在numeric头文件中定义. 假设vec是一个int型的vector对象,下面的代码: //sum the elements in vec starting the summation wit ...

  7. C++ STL算法系列1---count函数

    一.count函数 algorithm头文件定义了一个count的函数,其功能类似于find.这个函数使用一对迭代器和一个值做参数,返回这个值出现次数的统计结果. 编写程序读取一系列int型数据,并将 ...

  8. C++ STL算法之:copy

    C++ STL算法:copy 目录(?)[+] 前面十二个算法所展现的都属于非变易算法(Non-mutating algorithms)系列,现在我们来看看变易算法.所谓变易算法(Mutating a ...

  9. 实战c++中的vector系列--vector的遍历(stl算法、vector迭代器(不要在循环中推断不等于end())、operator[])

    遍历一个vector容器有非常多种方法.使用起来也是仁者见仁. 通过索引遍历: for (i = 0; i<v.size(); i++) { cout << v[i] << ...

随机推荐

  1. Hadoop基础教程-运行环境搭建

    一.Hadoop是什么 一个分布式系统基础架构,由Apache基金会所开发.用户可以在不了解分布式底层细节的情况下,开发分布式程序.充分利用集群的威力进行高速运算和存储. Hadoop实现了一个分布式 ...

  2. jquery.lazyload用法

    lazyload是jquery的插件,作为延迟加载图片,减压服务器压力. 如何使用: 先把 <script src="jquery.js" type="text/j ...

  3. outlook圆角table

    <table cellpadding="0" cellspacing="0" border="0" width="800&q ...

  4. centos使用fuse挂载NTFS

    FUSE:用户空间文件系统(Filesystem in Userspace),是Linux 中用于挂载某些网络空间,如SSH,到本地文件系统的模块.如果装的是双系统,centOS并不支持ntfs分区, ...

  5. [转]C:int型指针

    开源中国:http://my.oschina.net/lotte1699/blog/142538 网页快照:http://www.piaocafe.com/295977937/139381567037 ...

  6. MYSQL语句中SELECT语句及其子句的执行顺序

    SELECT语句的执行的逻辑查询处理步骤:(8)SELECT (9)DISTINCT(11)<TOP_specification> <select_list>(1)FROM & ...

  7. windows下安装python,安装框架django。

    第一步:  首先下载python安装包: 第二步:安装          双击安装包,安装程序.           这里安装到C盘   文件夹命名为  python33. 正在安装......... ...

  8. [HDOJ2512]一卡通大冒险(DP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2512 给一个数n,问1~n这n个数内的划分.设dp(i,j)为i划分为j个集合时有多少个. 初始化条件 ...

  9. Hadoop集群(第10期副刊)_常用MySQL数据库命令

    1.系统管理 1.1 连接MySQL 格式: mysql -h主机地址 -u用户名 -p用户密码 举例: 例1:连接到本机上的MySQL. 首先在打开DOS窗口,然后进入目录 mysqlbin,再键入 ...

  10. Cocos2d-x 开发手记

    1.所有的源文件统一新建到Classes里,否则无法找到源文件,这样也便于跨平台编译   2.绘图坐标系,与opengl采用相同坐标系,左下角为原点 纹理坐标系,以左上角为原点   3.最近有在学习C ...