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. Spring框架学习之第1节

    spring快速入门 ①   spring是什么? Struts是web框架(jsp/action/actionform) hibernate是orm框架(对象和关系映射框架),处于持久层 sprin ...

  2. IOS 的loadView 及使用loadView中初始化View注意的问题。(死循环并不可怕)

    在XCode 4.2后,我基本上的应用都不使用Xib文件了,虽然xib文件有很多好趣,可以快速免代码构建视窗,可以减少好多代码构建带来的麻烦,其实能用xib还是不错的,主要是我的机器打开xib来编辑时 ...

  3. CreateFile,WriteFile,ReadFile

    注意: CreateFile 跟 fopen 不同,打开文件时不区分 文本方式 或 二进制 方式 ReadFile 或 WriteFile 都是对二进制数据进行操作 HANDLE WINAPI Cre ...

  4. mac 下tomcat启动报错 unknown host

    解决方法:sudo scutil --set HostName localhost

  5. php mysql_insert_id()

    mysql_insert_id mysql_insert_id()返回给定的 link_identifier中上一步 INSERT 查询中产生的 AUTO_INCREMENT 的 ID 号.如果没有指 ...

  6. Maven+Spring+MVC结构中,jetty/tomcat是如何启动项目的[转]

    针对maven配置的Spring+MVC项目,我们用Maven自带的jetty和tomcat插件进行调试,这很方便.但是调试时,这些插件所启动的web服务器,是如何来将我们的工程作为一个web项目启动 ...

  7. eclipse启动报错 java was started but returned exit code = -805306369

    前几天还工作的好好的eclipse昨天下午启动时突然报错 报错如图: 妹的,好端端突然报错,非常奇怪,关键还是返回一堆看不懂的东西,细看都是eclipse.ini里面的配置信息,看到熟悉的jdk,误以 ...

  8. 《c程序设计语言》读书笔记-十六位进制数转十进制

    #include <stdio.h> #include <stdio.h> int htoi(char s[]); main() { char s1[] = "10& ...

  9. MTK android flash配置

    关于6573集成MCP nandflash的方法,driver_allinone 和Memory Customer Document pdf的说明里面漏了很多细节.在此补上. 1.首先确认flash型 ...

  10. 好,开始没做出来 guess-number-higher-or-lower-ii

    https://leetcode.com/mockinterview/session/result/xsicjnm/ https://leetcode.com/problems/guess-numbe ...