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

相关文章

PostgreSQL中rank()窗口函数实用指南与示例

《PostgreSQL中rank()窗口函数实用指南与示例》在数据分析和数据库管理中,经常需要对数据进行排名操作,PostgreSQL提供了强大的窗口函数rank(),可以方便地对结果集中的行进行排名... 目录一、rank()函数简介二、基础示例:部门内员工薪资排名示例数据排名查询三、高级应用示例1. 每

全面掌握 SQL 中的 DATEDIFF函数及用法最佳实践

《全面掌握SQL中的DATEDIFF函数及用法最佳实践》本文解析DATEDIFF在不同数据库中的差异,强调其边界计算原理,探讨应用场景及陷阱,推荐根据需求选择TIMESTAMPDIFF或inte... 目录1. 核心概念:DATEDIFF 究竟在计算什么?2. 主流数据库中的 DATEDIFF 实现2.1

MySQL中的LENGTH()函数用法详解与实例分析

《MySQL中的LENGTH()函数用法详解与实例分析》MySQLLENGTH()函数用于计算字符串的字节长度,区别于CHAR_LENGTH()的字符长度,适用于多字节字符集(如UTF-8)的数据验证... 目录1. LENGTH()函数的基本语法2. LENGTH()函数的返回值2.1 示例1:计算字符串

Java中的数组与集合基本用法详解

《Java中的数组与集合基本用法详解》本文介绍了Java数组和集合框架的基础知识,数组部分涵盖了一维、二维及多维数组的声明、初始化、访问与遍历方法,以及Arrays类的常用操作,对Java数组与集合相... 目录一、Java数组基础1.1 数组结构概述1.2 一维数组1.2.1 声明与初始化1.2.2 访问

MySQL 中的 CAST 函数详解及常见用法

《MySQL中的CAST函数详解及常见用法》CAST函数是MySQL中用于数据类型转换的重要函数,它允许你将一个值从一种数据类型转换为另一种数据类型,本文给大家介绍MySQL中的CAST... 目录mysql 中的 CAST 函数详解一、基本语法二、支持的数据类型三、常见用法示例1. 字符串转数字2. 数字

Python中你不知道的gzip高级用法分享

《Python中你不知道的gzip高级用法分享》在当今大数据时代,数据存储和传输成本已成为每个开发者必须考虑的问题,Python内置的gzip模块提供了一种简单高效的解决方案,下面小编就来和大家详细讲... 目录前言:为什么数据压缩如此重要1. gzip 模块基础介绍2. 基本压缩与解压缩操作2.1 压缩文

Python内置函数之classmethod函数使用详解

《Python内置函数之classmethod函数使用详解》:本文主要介绍Python内置函数之classmethod函数使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录1. 类方法定义与基本语法2. 类方法 vs 实例方法 vs 静态方法3. 核心特性与用法(1编程客

Python函数作用域示例详解

《Python函数作用域示例详解》本文介绍了Python中的LEGB作用域规则,详细解析了变量查找的四个层级,通过具体代码示例,展示了各层级的变量访问规则和特性,对python函数作用域相关知识感兴趣... 目录一、LEGB 规则二、作用域实例2.1 局部作用域(Local)2.2 闭包作用域(Enclos

解读GC日志中的各项指标用法

《解读GC日志中的各项指标用法》:本文主要介绍GC日志中的各项指标用法,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、基础 GC 日志格式(以 G1 为例)1. Minor GC 日志2. Full GC 日志二、关键指标解析1. GC 类型与触发原因2. 堆

MySQL数据库中ENUM的用法是什么详解

《MySQL数据库中ENUM的用法是什么详解》ENUM是一个字符串对象,用于指定一组预定义的值,并可在创建表时使用,下面:本文主要介绍MySQL数据库中ENUM的用法是什么的相关资料,文中通过代码... 目录mysql 中 ENUM 的用法一、ENUM 的定义与语法二、ENUM 的特点三、ENUM 的用法1