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

相关文章

JavaScript中的reduce方法执行过程、使用场景及进阶用法

《JavaScript中的reduce方法执行过程、使用场景及进阶用法》:本文主要介绍JavaScript中的reduce方法执行过程、使用场景及进阶用法的相关资料,reduce是JavaScri... 目录1. 什么是reduce2. reduce语法2.1 语法2.2 参数说明3. reduce执行过程

Python itertools中accumulate函数用法及使用运用详细讲解

《Pythonitertools中accumulate函数用法及使用运用详细讲解》:本文主要介绍Python的itertools库中的accumulate函数,该函数可以计算累积和或通过指定函数... 目录1.1前言:1.2定义:1.3衍生用法:1.3Leetcode的实际运用:总结 1.1前言:本文将详

MyBatis-Flex BaseMapper的接口基本用法小结

《MyBatis-FlexBaseMapper的接口基本用法小结》本文主要介绍了MyBatis-FlexBaseMapper的接口基本用法小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具... 目录MyBATis-Flex简单介绍特性基础方法INSERT① insert② insertSelec

轻松上手MYSQL之JSON函数实现高效数据查询与操作

《轻松上手MYSQL之JSON函数实现高效数据查询与操作》:本文主要介绍轻松上手MYSQL之JSON函数实现高效数据查询与操作的相关资料,MySQL提供了多个JSON函数,用于处理和查询JSON数... 目录一、jsON_EXTRACT 提取指定数据二、JSON_UNQUOTE 取消双引号三、JSON_KE

MySQL数据库函数之JSON_EXTRACT示例代码

《MySQL数据库函数之JSON_EXTRACT示例代码》:本文主要介绍MySQL数据库函数之JSON_EXTRACT的相关资料,JSON_EXTRACT()函数用于从JSON文档中提取值,支持对... 目录前言基本语法路径表达式示例示例 1: 提取简单值示例 2: 提取嵌套值示例 3: 提取数组中的值注意

深入解析Spring TransactionTemplate 高级用法(示例代码)

《深入解析SpringTransactionTemplate高级用法(示例代码)》TransactionTemplate是Spring框架中一个强大的工具,它允许开发者以编程方式控制事务,通过... 目录1. TransactionTemplate 的核心概念2. 核心接口和类3. TransactionT

数据库使用之union、union all、各种join的用法区别解析

《数据库使用之union、unionall、各种join的用法区别解析》:本文主要介绍SQL中的Union和UnionAll的区别,包括去重与否以及使用时的注意事项,还详细解释了Join关键字,... 目录一、Union 和Union All1、区别:2、注意点:3、具体举例二、Join关键字的区别&php

Java function函数式接口的使用方法与实例

《Javafunction函数式接口的使用方法与实例》:本文主要介绍Javafunction函数式接口的使用方法与实例,函数式接口如一支未完成的诗篇,用Lambda表达式作韵脚,将代码的机械美感... 目录引言-当代码遇见诗性一、函数式接口的生物学解构1.1 函数式接口的基因密码1.2 六大核心接口的形态学

oracle中exists和not exists用法举例详解

《oracle中exists和notexists用法举例详解》:本文主要介绍oracle中exists和notexists用法的相关资料,EXISTS用于检测子查询是否返回任何行,而NOTE... 目录基本概念:举例语法pub_name总结 exists (sql 返回结果集为真)not exists (s

Oracle的to_date()函数详解

《Oracle的to_date()函数详解》Oracle的to_date()函数用于日期格式转换,需要注意Oracle中不区分大小写的MM和mm格式代码,应使用mi代替分钟,此外,Oracle还支持毫... 目录oracle的to_date()函数一.在使用Oracle的to_date函数来做日期转换二.日