描述:
寻找两个容器出现不同元素的首个位置。
用 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的元素数量要大于等于容器1
auto 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,值:4
int nIndex2 = std::distance(std::begin(vecInt2), pair.second);
int nValue2 = *pair.second;
std::cout << "容器2首个不同元素索引:" << nIndex2 << ",值:" << nValue2 << std::endl;
//容器2首个不同元素索引:3,值:5
return 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,值:0
int nIndex2 = std::distance(std::begin(v2), pair.second);
SData value2 = *pair.second;
std::cout << "容器2首个不同元素索引:" << nIndex2 << ",值:" << value2.m_nData << std::endl;
//容器2首个不同元素索引:0,值:1
return 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,值:4
int nIndex2 = std::distance(std::begin(vecInt2), pair.second);
int nValue2 = *pair.second;
std::cout << "容器2首个不同元素索引:" << nIndex2 << ",值:" << nValue2 << std::endl;
//容器2首个不同元素索引:3,值:5
return 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,值:0
int nIndex2 = std::distance(std::begin(v2), pair.second);
SData value2 = *pair.second;
std::cout << "容器2首个不同元素索引:" << nIndex2 << ",值:" << value2.m_nData << std::endl;
//容器2首个不同元素索引:0,值:1
return 0;
}