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. MyBatis学习总结_11_MyBatis动态Sql语句

    MyBatis中对数据库的操作,有时要带一些条件,因此动态SQL语句非常有必要,下面就主要来讲讲几个常用的动态SQL语句的语法 MyBatis中用于实现动态SQL的元素主要有: if choose(w ...

  2. CentOS下的账户管理

    在Linux中,每个文件都分3类权限:账户本身的权限,账户所在群组的权限和其它权限.账户和群组是多对多的关系,即一个账户可以属于多个群组,一个群组可以包含多个账户.但是,对于每一个已登录的账户,只能存 ...

  3. Linux命令-date

    [root@localhost ~]# date 2016年 09月 07日 星期三 :: CST [root@localhost ~]# date "+%Y" [root@loc ...

  4. 自己动手实现java中cache

    实现思路: 创建一个静态Hashtable用于保存key和value,对于cache过期后的方法回调,在cache过期后,再访问cache的时候进行,避免了使用定时器轮询过期时间,进行cache清除的 ...

  5. 转载 a href=#与 a href=javascript:void(0) 的区别

    a href="#"> 点击链接后,页面会向上滚到页首,# 默认锚点为 #TOP <a href="javascript:void(0)" onCl ...

  6. Android 的 init.rc 文件简介【转】

    转自:http://blog.csdn.net/yimiyangguang1314/article/details/6268177 init.rc由许多的Action和Service组成.每一个语句占 ...

  7. Java安全编码之用户输入

    0x00 安全引言 1.传统Web应用与新兴移动应用 (1)传统Web应用:浏览器 HTTP 服务器(2)新兴移动应用:APP HTTP 服务器 从安全角度看,传统Web应用与新兴移动应用没有本质区别 ...

  8. Asterisk的配置详解

    Asterisk的配置文件都在/etc/asterisk目录下,重要的配置文件有: sip.conf                      sip电话基本配置 extensions.conf    ...

  9. [JWFD开源工作流]JWFD开源工作流官方下载内容更新

    在更新版的JWFD二次开发包中,我正在实现单线程的时钟控制器,动了下引擎的源代码,这个更新包主要是升级界面,内核代码,大家就不用升级了.. 代码提示: 请修改代码包中(org.jwfd.workflo ...

  10. hdoj - 1258 Sum It Up && hdoj - 1016 Prime Ring Problem (简单dfs)

    http://acm.hdu.edu.cn/showproblem.php?pid=1258 关键点就是一次递归里面一样的数字只能选一次. #include <cstdio> #inclu ...