C++ equal()函数详解

C++ equal()函数

C++ 算法函数equal()
C++ Algorithm equal()用法及代码示例

C++ 算法equal()函数比较两个容器中的元素,如果找到两个容器中的所有元素,则返回true值匹配

第一个范围从[first1,last1)开始,第二个范围从 first2开始

cpp 复制代码
template <class InputIterator1, class InputIterator2, class BinaryPredicate>
bool equal(InputIterator1 first1, InputIterator1 last1,
   InputIterator2 first2, BinaryPredicate pred);
  • first1 : 它是[first1,last1)的第一个元素的输入迭代器
  • last1 : 它是[first1,last1)的last元素的输入迭代器
  • first2 : 它是第一个元素的输入迭代器。 [first2,last2)的元素
  • pred : 它是一个二进制函数,接受两个元素作为参数并执行该函数设计的任务
  • 如果两个容器中的所有元素都匹配,则该函数返回值true,否则返回false
cpp 复制代码
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
bool newpredicate(int m, int n)
{
    return(m==n);
}
int main()
{
    int newints[]={20,40,60,80,100};
    std::vector<int> newvector(newints, newints+5);
    if(std::equal(newvector.begin(),newvector.end(),newints))
    std::cout<<"Both the containers have matching elements.\n";
    else
    std::cout<<"Both the containers have difference elements.\n";
    newvector[3]=81;
    if(std::equal(newvector.begin(),newvector.end(),newints,newpredicate))
    std::cout<<"Both the containers have equal containers.\n";
    else
    std::cout<<"Both the containers do not have equal elements. \n";
    return 0;
}

输出:

cpp 复制代码
Both the containers have matching elements.
Both the containers do not have equal elements.
相关推荐
clever1013 分钟前
在QtCreator 4.10.2中调试qt程序qDebug()输出中文为乱码问题的解决
开发语言·qt
测试开发Kevin25 分钟前
小tip:换行符CRLF 和 LF 的区别以及二者在实际项目中的影响
java·开发语言·python
松☆1 小时前
Dart 核心语法精讲:从空安全到流程控制(3)
android·java·开发语言
编码者卢布1 小时前
【App Service】Java应用上传文件功能部署在App Service Windows上报错 413 Payload Too Large
java·开发语言·windows
kaikaile19951 小时前
结构风荷载理论与Matlab计算
开发语言·matlab
切糕师学AI2 小时前
ARM 汇编器中的伪指令(Assembler Directives)
开发语言·arm开发·c#
No0d1es2 小时前
电子学会青少年软件编程(C语言)等级考试试卷(三级)2025年12月
c语言·c++·青少年编程·电子学会·三级
吕司2 小时前
Qt的信号与槽
开发语言·qt
bjxiaxueliang3 小时前
一文掌握C/C++命名规范:风格、规则与实践详解
c语言·开发语言·c++
玄〤3 小时前
Java 大数据量输入输出优化方案详解:从 Scanner 到手写快读(含漫画解析)
java·开发语言·笔记·算法