C++11标准模板(STL)- 算法(std::mismatch)

定义于头文件 <algorithm>

寻找两个范围出现不同的首个位置

std::mismatch
template< class InputIt1, class InputIt2 >

std::pair<InputIt1,InputIt2>
    mismatch( InputIt1 first1, InputIt1 last1,

              InputIt2 first2 );
(1)(C++20 前)
template< class InputIt1, class InputIt2 >

constexpr std::pair<InputIt1,InputIt2>
              mismatch( InputIt1 first1, InputIt1 last1,

                        InputIt2 first2 );
(C++20 起)
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2 >

std::pair<ForwardIt1,ForwardIt2>
    mismatch( ExecutionPolicy&& policy, ForwardIt1 first1, ForwardIt1 last1,

              ForwardIt2 first2 );
(2)(C++17 起)
(3)
template< class InputIt1, class InputIt2, class BinaryPredicate >

std::pair<InputIt1,InputIt2>
    mismatch( InputIt1 first1, InputIt1 last1,
              InputIt2 first2,

              BinaryPredicate p );
(C++20 前)
template< class InputIt1, class InputIt2, class BinaryPredicate >

constexpr std::pair<InputIt1,InputIt2>
              mismatch( InputIt1 first1, InputIt1 last1,
                        InputIt2 first2,

                        BinaryPredicate p );
(C++20 起)
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class BinaryPredicate >

std::pair<ForwardIt1,ForwardIt2>
    mismatch( ExecutionPolicy&& policy, ForwardIt1 first1, ForwardIt1 last1,
              ForwardIt2 first2,

              BinaryPredicate p );
(4)(C++17 起)
template< class InputIt1, class InputIt2 >

std::pair<InputIt1,InputIt2>
    mismatch( InputIt1 first1, InputIt1 last1,

              InputIt2 first2, InputIt2 last2 );
(5)(C++14 起)
(C++20 前)
template< class InputIt1, class InputIt2 >

constexpr std::pair<InputIt1,InputIt2>
              mismatch( InputIt1 first1, InputIt1 last1,

                        InputIt2 first2, InputIt2 last2 );
(C++20 起)
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2 >

std::pair<ForwardIt1,ForwardIt2>
    mismatch( ExecutionPolicy&& policy, ForwardIt1 first1, ForwardIt1 last1,

              ForwardIt2 first2, ForwardIt2 last2 );
(6)(C++17 起)
(7)
template< class InputIt1, class InputIt2, class BinaryPredicate >

std::pair<InputIt1,InputIt2>
    mismatch( InputIt1 first1, InputIt1 last1,
              InputIt2 first2, InputIt2 last2,

              BinaryPredicate p );
(C++14 起)
(C++20 前)
template< class InputIt1, class InputIt2, class BinaryPredicate >

constexpr std::pair<InputIt1,InputIt2>
              mismatch( InputIt1 first1, InputIt1 last1,
                        InputIt2 first2, InputIt2 last2,

                        BinaryPredicate p );
(C++20 起)
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class BinaryPredicate >

std::pair<ForwardIt1,ForwardIt2>
    mismatch( ExecutionPolicy&& policy, ForwardIt1 first1, ForwardIt1 last1,
              ForwardIt2 first2, ForwardIt2 last2,

              BinaryPredicate p );
(8)(C++17 起)

返回来自二个范围:一个以 [first1, last1) 定义而另一个以 [first2,last2) 定义,的首个不匹配对。若不提供 last2 (重载 (1-4) ),则它指代 first2 + (last1 - first1)

1,5) 用 operator== 比较元素。

3,7) 用给定的二元谓词 p 比较元素。

2,4,6,8) 同 (1,3,5,7) ,但按照 policy 执行。这些重载仅若 std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> 为 true 才参与重载决议。

参数

first1, last1-第一元素范围
first2, last2-第二元素范围
policy-所用的执行策略。细节见执行策略。
p-若元素应被当做相等则返回 ​true 的二元谓词。

谓词函数的签名应等价于如下:

 bool pred(const Type1 &a, const Type2 &b);

虽然签名不必有 const & ,函数也不能修改传递给它的对象,而且必须接受(可为 const 的)类型 Type1Type2 的值,无关乎值类别(从而不允许 Type1 & ,亦不允许 Type1 ,除非 Type1 的移动等价于复制 (C++11 起))。
类型 Type1 与 Type2 必须使得 InputIt1 与 InputIt2 类型的对象在解引用后分别能隐式转换到 Type1 与 Type2 。 ​

类型要求
- InputIt1 必须满足遗留输入迭代器 (LegacyInputIterator) 的要求。
- InputIt2 必须满足遗留输入迭代器 (LegacyInputIterator) 的要求。
- ForwardIt1 必须满足遗留向前迭代器 (LegacyForwardIterator) 的要求。
- ForwardIt2 必须满足遗留向前迭代器 (LegacyForwardIterator) 的要求。
- BinaryPredicate 必须满足二元谓词 (BinaryPredicate) 的要求。

返回值

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

若比较抵达 last1 时未找到不匹配,则 pair 保有 last1 和来自第二范围的对应迭代器。若第二范围短于第一范围则行为未定义。

(C++14 前)

若比较抵达 last1last2 中首先发生者时未找到不匹配,则 pair 保有尾迭代器和来自另一范围的对应迭代器。

(C++14 起)

复杂度

1-4) 至多 last1 - first1 次应用 operator== 或谓词 p

5-8) 至多 min(last1 - first1, last2 - first2) 次应用 operator== 或谓词 p

异常

拥有名为 ExecutionPolicy 的模板形参的重载按下列方式报告错误:

  • 若作为算法一部分调用的函数的执行抛出异常,且 ExecutionPolicy 为标准策略之一,则调用 std::terminate 。对于任何其他 ExecutionPolicy ,行为是实现定义的。
  • 若算法无法分配内存,则抛出 std::bad_alloc 。

可能的实现

 版本一

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

调用示例

#include <iostream>
#include <string>
#include <algorithm>

std::string mirror_ends(const std::string& in)
{
    auto mit = std::mismatch(in.begin(), in.end(), in.rbegin());
//    std::cout << "mirror_ends:  " << *mit.second << std::endl;
//    std::cout << "mirror_ends:  " << *mit.first << std::endl;
    return std::string(in.begin(), mit.first);
}

std::string mirror_ends_p(const std::string& in)
{
    auto func = [](char a, char b)
    {
        return a == b;
    };
    auto mit = std::mismatch(in.begin(), in.end(), in.rbegin(), func);
    return std::string(in.rbegin(), mit.second);
}

int main()
{
    std::cout << "mirror_ends(abXYZba)      " << mirror_ends("abXYZba") << '\n'
              << "mirror_ends(abca)         " << mirror_ends("abca") << '\n'
              << "mirror_ends(aba)          " << mirror_ends("aba") << "\n\n";

    std::cout << "mirror_ends_p(abXYZba)    " << mirror_ends_p("abXYZba") << '\n'
              << "mirror_ends_p(abca)       " << mirror_ends_p("abca") << '\n'
              << "mirror_ends_p(aba)        " << mirror_ends_p("aba") << '\n';
}

输出

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值