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. [转]ArcGIS计算图斑的四邻坐标(XMin,XMax,YMin,YMax)

    1.背景: 在国土,调查等行业业务里面经常有需要计算某个图斑的四邻坐标,即xmax,xmin,ymin,ymax;也就是常说的MBR(最小外包矩形),本教程演示如何计算一个shapefile文件上的图 ...

  2. LINUX下的MYSQL默认表名区分大小写

    让MYSQL不区分表名大小写的方法其实很简单: 1.用ROOT登录,修改/etc/my.cnf,该文件的路径也可能是/usr/my.cnf. 2.加入一行:lower_case_table_names ...

  3. 分布式缓存系统热点key解决方案

    1. 读访问量高(万级别及以上),读访问远大于写访问 这种情况典型的业务场景之一就是存储配置信息,配置信息一般数据量较小,更新频率低,但读访问量高.这种场景有两种方法优化: a. 对同一个key存储多 ...

  4. poj 1180 斜率优化dp

    这个题目要是顺着dp的话很难做,但是倒着推就很容易退出比较简单的关系式了. dp[i]=min(dp[u]+(sum[u-1]-sum[i-1]+s)*f[i]);dp[i]代表从i到结尾需要花费的代 ...

  5. 关于Linux系统调用,内核函数【转】

    转自:http://blog.csdn.net/ubuntulover/article/details/5988220 早上听人说到某个程序的一部分是内核态,另一部分是用户态,需要怎么怎么.当时突然想 ...

  6. leetcode:Palindrome Linked List

    Given a singly linked list, determine if it is a palindrome. Follow up:Could you do it in O(n) time ...

  7. IE JS编程需注意的内存释放问题

    1.给DOM对象添加的属性是一个对象的引用.范例:var MyObject = {};document.getElementById('myDiv').myProp = MyObject;解决方法:在 ...

  8. 如何将SD卡或者TF卡的debian系统刷入nand

    1. 在windows端下载辅助文件: a) http://dl.cubieforums.com/loz/boot_partition/bootloader/cubie_nand_uboot_part ...

  9. android 单词

    inflate: 胀, 膨, 通货膨胀, 膨胀系数

  10. tarjan总结

    先说一下割点跟割边吧. 割桥就是如果一个连通图里删除这条边之后,这个图会变成两个连通图,那么这条边就称之为割桥. 这是割桥的代码,里面呆着lca求法. 割点和割桥的就是用一个时间戳和回到祖先确定. 用 ...