描述:
       寻找两个容器出现不同元素的首个位置。
       用 operator== 比较元素。

定义:

template< class InputIt1, class InputIt2 >
std::pair<InputIt1,InputIt2>mismatch( InputIt1 first1, InputIt1 last1,InputIt2 first2 );template< class InputIt1, class InputIt2 >
constexpr std::pair<InputIt1,InputIt2>mismatch( InputIt1 first1, InputIt1 last1,InputIt2 first2 );
template< class InputIt1, class InputIt2, class BinaryPredicate >
std::pair<InputIt1,InputIt2>mismatch( InputIt1 first1, InputIt1 last1,InputIt2 first2,BinaryPredicate p );template< class InputIt1, class InputIt2, class BinaryPredicate >
constexpr std::pair<InputIt1,InputIt2>mismatch( InputIt1 first1, InputIt1 last1,InputIt2 first2,BinaryPredicate p );
template< class InputIt1, class InputIt2 >
std::pair<InputIt1,InputIt2>mismatch( InputIt1 first1, InputIt1 last1,InputIt2 first2, InputIt2 last2 );template< class InputIt1, class InputIt2 >
constexpr std::pair<InputIt1,InputIt2>mismatch( InputIt1 first1, InputIt1 last1,InputIt2 first2, InputIt2 last2 );
template< class InputIt1, class InputIt2, class BinaryPredicate >
std::pair<InputIt1,InputIt2>mismatch( InputIt1 first1, InputIt1 last1,InputIt2 first2, InputIt2 last2,BinaryPredicate p );template< class InputIt1, class InputIt2, class BinaryPredicate >
constexpr std::pair<InputIt1,InputIt2>mismatch( InputIt1 first1, InputIt1 last1,InputIt2 first2, InputIt2 last2,BinaryPredicate p );

可能的实现:

template<class InputIt1, class InputIt2>
std::pair<InputIt1, InputIt2>mismatch(InputIt1 first1, InputIt1 last1, InputIt2 first2)
{while (first1 != last1 && *first1 == *first2) {++first1, ++first2;}return std::make_pair(first1, first2);
}
template<class InputIt1, class InputIt2, class BinaryPredicate>
std::pair<InputIt1, InputIt2>mismatch(InputIt1 first1, InputIt1 last1, InputIt2 first2, BinaryPredicate p)
{while (first1 != last1 && p(*first1, *first2)) {++first1, ++first2;}return std::make_pair(first1, first2);
}
template<class InputIt1, class InputIt2>
std::pair<InputIt1, InputIt2>mismatch(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2)
{while (first1 != last1 && first2 != last2 && *first1 == *first2) {++first1, ++first2;}return std::make_pair(first1, first2);
}
template<class InputIt1, class InputIt2, class BinaryPredicate>
std::pair<InputIt1, InputIt2>mismatch(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, BinaryPredicate p)
{while (first1 != last1 && first2 != last2 && p(*first1, *first2)) {++first1, ++first2;}return std::make_pair(first1, first2);
}

参数:
       first1, last1 - 第一元素范围
       first2, last2 - 第二元素范围
       p - 若元素应被当做相等则返回 ​true 的二元谓词。

返回值:
       返回指向首二个不相等元素的迭代器的 std::pair 。

示例:

#include <iostream>
#include <algorithm>
#include <vector>int main()
{std::vector<int> vecInt1 = { 1,2,3,4,5,6 };std::vector<int> vecInt2 = { 1,2,3,5,6,7};//容器2的元素数量要大于等于容器1auto pair = std::mismatch(vecInt1.begin(), vecInt1.end(), vecInt2.begin());int nIndex1 = std::distance(std::begin(vecInt1), pair.first);int nValue1 = *pair.first;std::cout << "容器1首个不同元素索引:" << nIndex1 << ",值:" << nValue1 << std::endl;//容器1首个不同元素索引:3,值:4int nIndex2 = std::distance(std::begin(vecInt2), pair.second);int nValue2 = *pair.second;std::cout << "容器2首个不同元素索引:" << nIndex2 << ",值:" << nValue2 << std::endl;//容器2首个不同元素索引:3,值:5return 0;
}
#include <iostream>
#include <algorithm>
#include <vector>struct SData
{int m_nData;double m_dData;SData(int nData, double dData):m_nData(nData),m_dData(dData){}
};int main()
{std::vector<SData> v1;for (int i = 0; i < 5; i++){v1.push_back(SData(i,i));}std::vector<SData> v2;for (int i = 1; i < 6; i++){v2.push_back(SData(i, i));}//容器2的元素数量要大于等于容器1//使用lambda表达式指定条件(一般用于自定义元素)auto pair = std::mismatch(v1.begin(), v1.end(), v2.begin(), [](const SData& data1,const SData& data2) {return data1.m_nData == data2.m_nData;});int nIndex1 = std::distance(std::begin(v1), pair.first);SData value1 = *pair.first;std::cout << "容器1首个不同元素索引:" << nIndex1 << ",值:" << value1.m_nData << std::endl;//容器1首个不同元素索引:0,值:0int nIndex2 = std::distance(std::begin(v2), pair.second);SData value2 = *pair.second;std::cout << "容器2首个不同元素索引:" << nIndex2 << ",值:" << value2.m_nData << std::endl;//容器2首个不同元素索引:0,值:1return 0;
}
#include <iostream>
#include <algorithm>
#include <vector>int main()
{std::vector<int> vecInt1 = { 1,2,3,4,5,6 };std::vector<int> vecInt2 = { 1,2,3,5,6 };auto pair = std::mismatch(vecInt1.begin(), vecInt1.end(), vecInt2.begin(), vecInt2.end());int nIndex1 = std::distance(std::begin(vecInt1), pair.first);int nValue1 = *pair.first;std::cout << "容器1首个不同元素索引:" << nIndex1 << ",值:" << nValue1 << std::endl;//容器1首个不同元素索引:3,值:4int nIndex2 = std::distance(std::begin(vecInt2), pair.second);int nValue2 = *pair.second;std::cout << "容器2首个不同元素索引:" << nIndex2 << ",值:" << nValue2 << std::endl;//容器2首个不同元素索引:3,值:5return 0;
}
#include <iostream>
#include <algorithm>
#include <vector>struct SData
{int m_nData;double m_dData;SData(int nData, double dData):m_nData(nData),m_dData(dData){}
};int main()
{std::vector<SData> v1;for (int i = 0; i < 5; i++){v1.push_back(SData(i,i));}std::vector<SData> v2;for (int i = 1; i < 5; i++){v2.push_back(SData(i, i));}//使用lambda表达式指定条件(一般用于自定义元素)auto pair = std::mismatch(v1.begin(), v1.end(), v2.begin(),v2.end(), [](const SData& data1,const SData& data2) {return data1.m_nData == data2.m_nData;});int nIndex1 = std::distance(std::begin(v1), pair.first);SData value1 = *pair.first;std::cout << "容器1首个不同元素索引:" << nIndex1 << ",值:" << value1.m_nData << std::endl;//容器1首个不同元素索引:0,值:0int nIndex2 = std::distance(std::begin(v2), pair.second);SData value2 = *pair.second;std::cout << "容器2首个不同元素索引:" << nIndex2 << ",值:" << value2.m_nData << std::endl;//容器2首个不同元素索引:0,值:1return 0;
}

std::mismatch 用法相关推荐

  1. boost::hana::ext::std::vector_tag用法的测试程序

    boost::hana::ext::std::vector_tag用法的测试程序 实现功能 C++实现代码 实现功能 boost::hana::ext::std::vector_tag用法的测试程序 ...

  2. std::map用法

    std::map用法 STL是标准C++系统的一组模板类,使用STL模板类最大的好处就是在各种C++编译器上都通用. 在STL模板类中,用于线性数据存储管理的类主要有vector, list, map ...

  3. std::tuple、std::tie(可用于结构体大小比较)、std::pair用法

    1.tuple应用: 解释:是一个元组,可包含无限多不同类型变量,pair的升级版,但没有pair得成员变量first.second. 1.1.代码: // tuple example #includ ...

  4. 转: std::string用法详解

    原文地址为: 转: std::string用法详解  C++中的string 类 简单介绍 前言: string 的角色 1 string 使用 1.1 充分使用string 操作符 1.2 眼花缭乱 ...

  5. 【C++】浮点数的std::fixed、std::setprecision()、std::setw()用法

    [C++]50.浮点数的std::fixed.std::setprecision().std::setw()用法 C++中std::setw()的用法 1. std::fixed() #include ...

  6. 【C++标准库】std::string用法指南源码剖析

    文章目录 1.ASCII码 (1)计算机如何表达字符 2.C 语言中的字符类型 char (1)思想:char 即整数 (3)C 语言帮手函数 (4)C语言中的字符串 (4)C 语言转义符 3.C++ ...

  7. std::function用法详解

    std::function用法详解 代码在:VCCommon/functionDemo std::function 简介 类模板 std :: function 是一个通用的多态函数包装器. std ...

  8. C++/C++11中std::string用法汇总

    C++/C++11中std::string是个模板类,它是一个标准库.使用string类型必须首先包含<string>头文件.作为标准库的一部分,string定义在命名空间std中. st ...

  9. c语言rank需要头文件吗,C++ std::rank用法及代码示例

    头文件中存在C++ STL的std::rank模板. C++ STL的std::rank模板用于查找类型T的等级.此函数返回类型T的等级. 头文件: #include 模板类别: template s ...

最新文章

  1. mysql server 80_mysql Host 'microsof-80f25e' is not allowed to connect to this MySQL server
  2. EasyUI + Bootstrap 界面整合
  3. 行为型模式:中介者模式
  4. 项目中使用RDLC报表
  5. 五种I/O 模式——阻塞(默认IO模式),非阻塞(常用语管道),I/O多路复用(IO多路复用的应用场景),信号I/O,异步I/O
  6. jqgrid使用小记
  7. Atitit zip压缩过滤器 的模块功能语实现attilax总结 1.1. 一般可以使用webserver自带的实现,。如果实现的不好或者不好配置的,或者需要精细化控制的,可以自己使用过滤器实现。
  8. php+jq+添加css,jq如何添加css样式?
  9. win 10 好吗?对比与ubuntu,对比于Mac呢?
  10. 至于你信不信,反正我信了
  11. 右键快捷菜单压缩文件的消失问题解决办法!
  12. 微信故障之后发生的三大怪现象
  13. 构建高并发高可用的电商平台架构实践
  14. 【目标检测】概念理解:region proposal、bounding box、anchor box、ground truth、IoU、NMS、RoI Pooling
  15. ffmpeg使用filter生成H264测试视频(带时间戳OSD)
  16. 学生信息管理系统开发
  17. C语言中的整型变量与实行常量
  18. c# 通过ImageSharp实现心电图数据保存为本地图片
  19. switch 生化危机启示录2 通关测评
  20. python中ceil函数是什么意思_python中ceil()函数用法

热门文章

  1. C语言基础知识复习(1)
  2. CAN总线通信学习笔记
  3. 联想Z5 Pro划时代旗舰发布 屏占比95.06%售价1998元起
  4. java检查中断_死神来了怎么判断之java线程中断
  5. 安卓AutoCompleteTextView的简单使用(搜索时弹出可选择项)
  6. java拆分日期_java实现日期拆分的方法
  7. 3.抽象类(shape)
  8. 仿权重8高收录面包网pc+手机苹果cmsv8影视网站含迅雷下载N430模板
  9. 一张图看清楚成功人士与失败人士的差别,成功人士的10个标志
  10. 工信部教育与考试中心-软件测试工程师考试题A卷-答