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.
相关推荐
Knight_AL几秒前
阿里《Java 开发手册》下的对象构建与赋值规范实践
java·开发语言
lsx2024062 分钟前
SQL LIKE 操作符详解
开发语言
微爱帮监所写信寄信9 分钟前
微爱帮监狱寄信写信工具照片高清处理技术架构
开发语言·人工智能·网络协议·微信·php
报错小能手20 分钟前
数据结构 字典树
开发语言·数据结构
XLYcmy29 分钟前
高级密码生成器程序详解:专门设计用于生成基于用户个人信息的密码猜测组合
开发语言·数据结构·python·网络安全·数据安全·源代码·口令安全
独自破碎E37 分钟前
Leetcode862和至少为K的最短子数组
java·开发语言
qq_3707730940 分钟前
x64dbg 脚本常用命令
开发语言·x64dbg
阿豪只会阿巴44 分钟前
【多喝热水系列】从零开始的ROS2之旅——Day4
c++·笔记·python·ros2
军军君011 小时前
Three.js基础功能学习五:雾与渲染目标
开发语言·前端·javascript·学习·3d·前端框架·three
charlie1145141911 小时前
FreeRTOS:软件定时器(Software Timers)与时间管理
开发语言·笔记·学习·freertos·实时操作系统·工程