前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C++ STL算法系列5---equal() , mismatch()

C++ STL算法系列5---equal() , mismatch()

作者头像
猿人谷
发布2018-01-17 09:32:02
6370
发布2018-01-17 09:32:02
举报
文章被收录于专栏:猿人谷猿人谷

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是等价的.

代码语言:javascript
复制
 1 // Illustrating the generic equal and mismatch algorithms
 2 #include <iostream>
 3 #include <cassert>
 4 #include <algorithm>
 5 #include <string>
 6 #include <list>
 7 #include <deque>
 8 #include <vector>
 9 using namespace std;
10 
11 int main()
12 {
13   cout << "Illustrating the generic equal "
14        << "and mismatch algorithms." << endl;
15   list<string> driver_list;
16   vector<string> vec;
17   deque<string> deq;
18 
19   driver_list.insert(driver_list.end(), "Clark");
20   driver_list.insert(driver_list.end(), "Rindt");
21   driver_list.insert(driver_list.end(), "Senna");
22 
23   vec.insert(vec.end(), "Clark");
24   vec.insert(vec.end(), "Rindt");
25   vec.insert(vec.end(), "Senna");
26   vec.insert(vec.end(), "Berger");
27 
28   deq.insert(deq.end(), "Clark");
29   deq.insert(deq.end(), "Berger");
30 
31   // Show that driver_list and the first 3 elements of
32   // vec are equal in all corresponding positions:
33   assert (equal(driver_list.begin(), driver_list.end(),
34                 vec.begin()));
35 
36   // Show that deq and the first 2 elements of driver_list
37   // are not equal in all corresponding positions:
38   assert (!equal(deq.begin(), deq.end(),
39                  driver_list.begin()));
40 
41   // Find the corresponding positions in deq and driver_list
42   // at which unequal elements first occur:
43   pair<deque<string>::iterator, list<string>::iterator>
44     pair1 = mismatch(deq.begin(), deq.end(),
45                      driver_list.begin());
46 
47   if (pair1.first != deq.end())
48     cout << "First disagreement in deq and driver_list:\n  "
49          << *(pair1.first) << " and " << *(pair1.second)
50          << endl;
51   return 0;
52 }

equal算法类似于mismatch,equal算法也是逐一比较两个序列的元素是否相等,只是equal函数的返回值为bool值 true/false,不是返回迭代器值。它有如下两个原型,如果迭代器区间[first1,last1)和迭代器区间[first2, first2+(last1 - first1))上的元素相等(或者满足二元谓词判断条件binary_pred) ,返回true,否则返回false。

  函数原型:

代码语言:javascript
复制
 1 template<class InputIterator1, class InputIterator2>
 2    bool equal(
 3       InputIterator1 _First1, 
 4       InputIterator1 _Last1, 
 5       InputIterator2 _First2
 6       );
 7 template<class InputIterator1, class InputIterator2, class BinaryPredicate>
 8    bool equal(
 9       InputIterator1 _First1, 
10       InputIterator1 _Last1, 
11       InputIterator2 _First2, 
12       BinaryPredicate _Comp
13       );
14  

example:

利用二元谓词判断条件absEqual,判断出两个vector向量容器的元素均绝对值相等。

代码语言:javascript
复制
 1 #include <algorithm>  
 2 #include <vector>  
 3 #include <iostream>  
 4   
 5 using namespace std;  
 6   
 7 bool absEqual(int a, int b)  
 8 {  
 9     return (a == abs(b) || b == abs(a)) ? true : false;  
10 }  
11   
12 int main()  
13 {  
14     vector<int> ivect1(5);  
15     vector<int> ivect2(5);  
16   
17     for (vector<int>::size_type i = 0; i < ivect1.size(); ++i)  
18     {  
19         ivect1[i] = i;  
20         ivect2[i] = (-1) * i;  
21     }  
22     if ( equal( ivect1.begin(), ivect1.end(), ivect2.begin(), absEqual ) )  
23     {  
24         cout << "ivect1 和 ivect2 元素的绝对值完全相等" << endl;  
25     }   
26     else  
27     {  
28         cout << "ivect1 和 ivect2 元素的绝对值不完全相等" << endl;  
29     }  
30     return 0;  
31 }  
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2013-08-17 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档