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.
相关推荐
艾莉丝努力练剑5 小时前
【C++:异常】C++ 异常处理完全指南:从理论到实践,深入理解栈展开与最佳实践
java·开发语言·c++·安全·c++11
快乐zbc11 小时前
【C++ 基础】:给定一个指针 p,你能判断它是否指向合法的对象吗?
c++
岁忧12 小时前
GoLang五种字符串拼接方式详解
开发语言·爬虫·golang
tyatyatya12 小时前
MATLAB基础数据类型教程:数值型/字符型/逻辑型/结构体/元胞数组全解析
开发语言·matlab
sulikey12 小时前
C++类和对象(下):初始化列表、static、友元、内部类等核心特性详解
c++·static·初始化列表·友元·匿名对象·内部类·编译器优化
心无旁骛~12 小时前
python多进程和多线程问题
开发语言·python
星云数灵12 小时前
使用Anaconda管理Python环境:安装与验证Pandas、NumPy、Matplotlib
开发语言·python·数据分析·pandas·教程·环境配置·anaconda
kaikaile199512 小时前
基于遗传算法的车辆路径问题(VRP)解决方案MATLAB实现
开发语言·人工智能·matlab
四问四不知13 小时前
Rust语言进阶(结构体)
开发语言·后端·rust
q***99413 小时前
index.php 和 php
开发语言·php