Equal()函数的用法

2024-03-07 19:08
文章标签 函数 用法 equal

本文主要是介绍Equal()函数的用法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

函数语法

 
1、template <class InputIterator1, class InputIterator2>bool equal ( InputIterator1 first1, InputIterator1 last1, InputIterator2 first2 );2、template <class InputIterator1, class InputIterator2, class BinaryPredicate>bool equal ( InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, BinaryPredicate pred );
函数用法:

测试是否在两个范围的元素都是平等的

Compares the elements in the range [first1,last1) with those in the range beginning at first2, and returns true if the elements in both ranges are considered equal.

The elements are compared by either applying the == comparison operator to each pair of corresponding elements, or the template parameter comp (for the second version).

这个函数模板的行为等于:

template <class InputIterator1, class InputIterator2>bool equal ( InputIterator1 first1, InputIterator1 last1, InputIterator2 first2 )
{while ( first1!=last1 ){if (*first1 != *first2)   // or: if (!pred(*first1,*first2)), for pred versionreturn false;++first1; ++first2;}return true;
}
参数注释:
   first1, last1:
      Forward iterators to the initial and final positions of the first sequence. The range used is [first1,last1), which contains all the elements between first1 and last1, including the element pointed by first1 but not the element pointed by last1.           

first2:

Forward iterator to the initial position of the second sequence. The comparison includes up to as many elements in this sequence as in the above sequence.

pred:

Binary predicate taking two elements as argument (one of each of the two sequences), and returning the result of the comparison between them, with true (non-zero) meaning that they are to be considered equal, and false (zero) that they are not-equal. This can either be a pointer to a function or an object whose class overloads operator().

返回值:
   
  1. #include<iostream>  
  2. #include<algorithm>  
  3. #include<vector>  
  4. using namespace std;  
  5. class student{  
  6.     public:  
  7.         int No,grade;  
  8.         student(int No,int grade):No(No),grade(grade){};  
  9.         bool operator==(student& s){  
  10.             return this->grade==s.grade;  
  11.         }  
  12. };  
  13. int main(){  
  14.     vector<student>v1;  
  15.     student s1(1000,100);  
  16.     student s2(1001,98);  
  17.     student s3(1002,96);  
  18.     student s4(1003,94);  
  19.     v1.push_back(s1);  
  20.     v1.push_back(s2);  
  21.     v1.push_back(s3);  
  22.     v1.push_back(s4);  
  23.     vector<student>v2;  
  24.     v2=v1;  
  25.     if(equal(v1.begin(),v1.end(),v2.begin())){  
  26.         cout<<"yes"<<endl;  
  27.     }  
  28. }  

true if all the elements in the range [first1,last1) compare equal to those of the range starting at first2, and false otherwise.

  1. #include<iostream>  
  2. #include<algorithm>  
  3. #include<vector>  
  4. using namespace std;  
  5. class student{  
  6.     public:  
  7.         int No,grade;  
  8.         student(int No,int grade):No(No),grade(grade){};  
  9.     /*  bool operator==(student& s){ 
  10.             return this->grade==s.grade; 
  11.         }*/  
  12. };  
  13. bool cmp(student& s1,student& s2){  
  14.     return s1.grade==s2.grade;  
  15. }  
  16. int main(){  
  17.     vector<student>v1;  
  18.     student s1(1000,100);  
  19.     student s2(1001,98);  
  20.     student s3(1002,96);  
  21.     student s4(1003,94);  
  22.     v1.push_back(s1);  
  23.     v1.push_back(s2);  
  24.     v1.push_back(s3);  
  25.     v1.push_back(s4);  
  26.     vector<student>v2;  
  27.     v2=v1;  
  28.     if(equal(v1.begin(),v1.end(),v2.begin(),cmp)){  
  29.         cout<<"yes"<<endl;  
  30.     }  
  31. }  

这篇关于Equal()函数的用法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/784522

相关文章

C语言中%zu的用法解读

《C语言中%zu的用法解读》size_t是无符号整数类型,用于表示对象大小或内存操作结果,%zu是C99标准中专为size_t设计的printf占位符,避免因类型不匹配导致错误,使用%u或%d可能引发... 目录size_t 类型与 %zu 占位符%zu 的用途替代占位符的风险兼容性说明其他相关占位符验证示

MySQL中EXISTS与IN用法使用与对比分析

《MySQL中EXISTS与IN用法使用与对比分析》在MySQL中,EXISTS和IN都用于子查询中根据另一个查询的结果来过滤主查询的记录,本文将基于工作原理、效率和应用场景进行全面对比... 目录一、基本用法详解1. IN 运算符2. EXISTS 运算符二、EXISTS 与 IN 的选择策略三、性能对比

MySQL常用字符串函数示例和场景介绍

《MySQL常用字符串函数示例和场景介绍》MySQL提供了丰富的字符串函数帮助我们高效地对字符串进行处理、转换和分析,本文我将全面且深入地介绍MySQL常用的字符串函数,并结合具体示例和场景,帮你熟练... 目录一、字符串函数概述1.1 字符串函数的作用1.2 字符串函数分类二、字符串长度与统计函数2.1

全面解析Golang 中的 Gorilla CORS 中间件正确用法

《全面解析Golang中的GorillaCORS中间件正确用法》Golang中使用gorilla/mux路由器配合rs/cors中间件库可以优雅地解决这个问题,然而,很多人刚开始使用时会遇到配... 目录如何让 golang 中的 Gorilla CORS 中间件正确工作一、基础依赖二、错误用法(很多人一开

Java Stream流之GroupBy的用法及应用场景

《JavaStream流之GroupBy的用法及应用场景》本教程将详细介绍如何在Java中使用Stream流的groupby方法,包括基本用法和一些常见的实际应用场景,感兴趣的朋友一起看看吧... 目录Java Stream流之GroupBy的用法1. 前言2. 基础概念什么是 GroupBy?Stream

python使用try函数详解

《python使用try函数详解》Pythontry语句用于异常处理,支持捕获特定/多种异常、else/final子句确保资源释放,结合with语句自动清理,可自定义异常及嵌套结构,灵活应对错误场景... 目录try 函数的基本语法捕获特定异常捕获多个异常使用 else 子句使用 finally 子句捕获所

Java Spring的依赖注入理解及@Autowired用法示例详解

《JavaSpring的依赖注入理解及@Autowired用法示例详解》文章介绍了Spring依赖注入(DI)的概念、三种实现方式(构造器、Setter、字段注入),区分了@Autowired(注入... 目录一、什么是依赖注入(DI)?1. 定义2. 举个例子二、依赖注入的几种方式1. 构造器注入(Con

详解MySQL中JSON数据类型用法及与传统JSON字符串对比

《详解MySQL中JSON数据类型用法及与传统JSON字符串对比》MySQL从5.7版本开始引入了JSON数据类型,专门用于存储JSON格式的数据,本文将为大家简单介绍一下MySQL中JSON数据类型... 目录前言基本用法jsON数据类型 vs 传统JSON字符串1. 存储方式2. 查询方式对比3. 索引

postgresql使用UUID函数的方法

《postgresql使用UUID函数的方法》本文给大家介绍postgresql使用UUID函数的方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录PostgreSQL有两种生成uuid的方法。可以先通过sql查看是否已安装扩展函数,和可以安装的扩展函数

MySQL字符串常用函数详解

《MySQL字符串常用函数详解》本文给大家介绍MySQL字符串常用函数,本文结合实例代码给大家介绍的非常详细,对大家学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录mysql字符串常用函数一、获取二、大小写转换三、拼接四、截取五、比较、反转、替换六、去空白、填充MySQL字符串常用函数一、