泛型算法(二十一)之比较算法

1、equal(inIter1Begin, inIter1End, inIter2Begin):比较两个序列的对应元素是否相等

    std::vector<int> c1 = {1, 2, 3, 4, 5};
    std::vector<int> c2 = {1, 2, 3, 4, 5};
    //比较c1,c2两个序列的对应元素是否相等
    bool is_equal = std::equal(c1.begin(), c1.end(), c2.begin());
    //输出
    std::cout << (int)is_equal;
    //打印结果:1

2、equal(inIter1Begin, inIter1End, inIter2Begin, binPred):重载版本,其中binPred是给定的相等比较函数。

自己实现binPred,向算法定制操作。

 3、lexicographical_compare(inIter1Begin, inIter1End, inIter2Begin, inIter2End):对两个序列做词典比较。两个序列的对应元素用<运算符比较。如果第一个序列在词典序下小于第二个序列,返回true

    std::vector<char> c1 = {'a', 'b', 'c', 'd'};
    std::vector<char> c2 = {'e', 'f', 'g', 'h'};

    bool i = std::lexicographical_compare(c1.begin(), c1.end(), c2.begin(), c2.end());
    std::cout << (int)i;
    //打印结果:1

4、lexicographical_compare(inIter1Begin, inIter1End, inIter2Begin, inIter2End, binPred):重载版本,其中binPred是给定的“小于”比较函数。

自己实现binPred,向算法定制操作。

5、mismatch(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2):比较两个序列的对应元素,返回用std::pair表示的第一处不匹配在两个序列的位置。比较时使用==运算符。

    std::vector<char> c1 = {'a', 'b', 'c', 'd'};
    std::vector<char> c2 = {'a', 'b', 'g', 'h'};

    auto p = std::mismatch(c1.begin(), c1.end(), c2.begin());
    std::cout << *p.first<<","<< *p.second;
    //打印结果:c,g

6、mismatch(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, BinaryPredicate pred):重载版本,其中pred是给定的“相等”比较函数。

自己实现pred,向算法定制操作。

原文地址:https://www.cnblogs.com/dongerlei/p/5144371.html