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.
相关推荐
xyq202439 分钟前
TypeScript中的String类型详解
开发语言
小糖学代码6 小时前
LLM系列:1.python入门:15.JSON 数据处理与操作
开发语言·python·json·aigc
handler017 小时前
从源码到二进制:深度拆解 Linux 下 C 程序的编译与链接全流程
linux·c语言·开发语言·c++·笔记·学习
小白学大数据7 小时前
现代Python爬虫开发范式:基于Asyncio的高可用架构实战
开发语言·爬虫·python·架构
渔舟小调7 小时前
P19 | 前端加密通信层 pikachuNetwork.js 完整实现
开发语言·前端·javascript
不爱吃炸鸡柳7 小时前
数据结构精讲:树 → 二叉树 → 堆 从入门到实战
开发语言·数据结构
网络安全许木7 小时前
自学渗透测试第21天(基础命令复盘与DVWA熟悉)
开发语言·网络安全·渗透测试·php
t***5447 小时前
如何在Dev-C++中使用Clang编译器
开发语言·c++
码界筑梦坊8 小时前
93-基于Python的中药药材数据可视化分析系统
开发语言·python·信息可视化
Qbw20048 小时前
【Linux】进程地址空间
linux·c++