C++ equal()函数详解

C++ equal()函数

C++ 算法函数equal()

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

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

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

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
#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;
}

输出:

Both the containers have matching elements.
Both the containers do not have equal elements.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Prejudices

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值