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

相关文章

Python中help()和dir()函数的使用

《Python中help()和dir()函数的使用》我们经常需要查看某个对象(如模块、类、函数等)的属性和方法,Python提供了两个内置函数help()和dir(),它们可以帮助我们快速了解代... 目录1. 引言2. help() 函数2.1 作用2.2 使用方法2.3 示例(1) 查看内置函数的帮助(

mapstruct中的@Mapper注解的基本用法

《mapstruct中的@Mapper注解的基本用法》在MapStruct中,@Mapper注解是核心注解之一,用于标记一个接口或抽象类为MapStruct的映射器(Mapper),本文给大家介绍ma... 目录1. 基本用法2. 常用属性3. 高级用法4. 注意事项5. 总结6. 编译异常处理在MapSt

C++ 函数 strftime 和时间格式示例详解

《C++函数strftime和时间格式示例详解》strftime是C/C++标准库中用于格式化日期和时间的函数,定义在ctime头文件中,它将tm结构体中的时间信息转换为指定格式的字符串,是处理... 目录C++ 函数 strftipythonme 详解一、函数原型二、功能描述三、格式字符串说明四、返回值五

java中long的一些常见用法

《java中long的一些常见用法》在Java中,long是一种基本数据类型,用于表示长整型数值,接下来通过本文给大家介绍java中long的一些常见用法,感兴趣的朋友一起看看吧... 在Java中,long是一种基本数据类型,用于表示长整型数值。它的取值范围比int更大,从-922337203685477

MyBatis ResultMap 的基本用法示例详解

《MyBatisResultMap的基本用法示例详解》在MyBatis中,resultMap用于定义数据库查询结果到Java对象属性的映射关系,本文给大家介绍MyBatisResultMap的基本... 目录MyBATis 中的 resultMap1. resultMap 的基本语法2. 简单的 resul

Python主动抛出异常的各种用法和场景分析

《Python主动抛出异常的各种用法和场景分析》在Python中,我们不仅可以捕获和处理异常,还可以主动抛出异常,也就是以类的方式自定义错误的类型和提示信息,这在编程中非常有用,下面我将详细解释主动抛... 目录一、为什么要主动抛出异常?二、基本语法:raise关键字基本示例三、raise的多种用法1. 抛

java中Optional的核心用法和最佳实践

《java中Optional的核心用法和最佳实践》Java8中Optional用于处理可能为null的值,减少空指针异常,:本文主要介绍java中Optional核心用法和最佳实践的相关资料,文中... 目录前言1. 创建 Optional 对象1.1 常规创建方式2. 访问 Optional 中的值2.1

git stash命令基本用法详解

《gitstash命令基本用法详解》gitstash是Git中一个非常有用的命令,它可以临时保存当前工作区的修改,让你可以切换到其他分支或者处理其他任务,而不需要提交这些还未完成的修改,这篇文章主要... 目录一、基本用法1. 保存当前修改(包括暂存区和工作区的内容)2. 查看保存了哪些 stash3. 恢

Python中bisect_left 函数实现高效插入与有序列表管理

《Python中bisect_left函数实现高效插入与有序列表管理》Python的bisect_left函数通过二分查找高效定位有序列表插入位置,与bisect_right的区别在于处理重复元素时... 目录一、bisect_left 基本介绍1.1 函数定义1.2 核心功能二、bisect_left 与

Python struct.unpack() 用法及常见错误详解

《Pythonstruct.unpack()用法及常见错误详解》struct.unpack()是Python中用于将二进制数据(字节序列)解析为Python数据类型的函数,通常与struct.pa... 目录一、函数语法二、格式字符串详解三、使用示例示例 1:解析整数和浮点数示例 2:解析字符串示例 3:解