STL-比较算法

如果这篇文章对比有帮助,请夸山君是最帅的程序员

//equal比较算法
//返回值:当且仅当第一个序列和第二个序列相同,返回true,否则返回false

    template<class InputIterator1, class InputIterator2>
    bool equal(
    InputIterator1 First1,
    InputIterator1 Last1,
    InputIterator2 First2
    );
    template<class InputIterator1, class InputIterator2, class BinaryPredicate>
    bool equal(
    InputIterator1 First1,
    InputIterator1 Last1,
    InputIterator2 First2,
    BinaryPredicate Comp
    );

    // C++14
    template<class InputIterator1, class InputIterator2>
    bool equal(
    InputIterator1 First1,
    InputIterator1 Last1,
    InputIterator2 First2,
    InputIterator2 Last2
    );

    template<class InputIterator1, class InputIterator2, class BinaryPredicate>
    bool equal(
    InputIterator1 First1,
    InputIterator1 Last1,
    InputIterator2 First2,
    InputIterator2 Last2,

    BinaryPredicate Comp
    );



    //misnatch 函数
    /* 
    返回值 pair里面存放两个迭代器,当且仅当
        first等于第一个序列的尾后指针
        second等于第二个序列的尾后指针
    表示两个序列完全相等
    否则两个序列不匹配
    函数原形*/
    template<class InputIterator1, class InputIterator2>
    pair<InputIterator1, InputIterator2> mismatch(
    InputIterator1 First1,
    InputIterator1 Last1,
    InputIterator2 First2
    );
    template<class InputIterator1, class InputIterator2, class BinaryPredicate>
    pair<InputIterator1, InputIterator2> mismatch(
    InputIterator1 First1,
    InputIterator1 Last1,
    InputIterator2 First2,
    BinaryPredicate Comp
    );

    //C++14
    template<class InputIterator1, class InputIterator2>
    pair<InputIterator1, InputIterator2> mismatch(
    InputIterator1 First1,
    InputIterator1 Last1,
    InputIterator2 First2,
    InputIterator2 Last2
    );
    template<class InputIterator1, class InputIterator2, class BinaryPredicate>
    pair<InputIterator1, InputIterator2> mismatch(
    InputIterator1 First1,
    InputIterator1 Last1,
    InputIterator2 First2,
    InputIterator2 Last2,
    BinaryPredicate Comp
    );



    //lexicographical_compare函数 //字典排序比较
    /*
    第一个序列小于等于第二个返回true,否则返回false*/
    template<class InputIterator1, class InputIterator2>
    bool lexicographical_compare(
    InputIterator1 _First1,
    InputIterator1 _Last1,
    InputIterator2 _First2,
    InputIterator2 _Last2
    );
    template<class InputIterator1, class InputIterator2, class BinaryPredicate>
    bool lexicographical_compare(
    InputIterator1 _First1,
    InputIterator1 _Last1,
    InputIterator2 _First2,
    InputIterator2 _Last2,
    BinaryPredicate _Comp
    );

比较算法示例

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
    vector<int> v1{ 0, 5, 10, 15, 20, 25 };
    vector<int> v2{ 0, 5, 10, 15, 20, 25 };
    vector<int> v3{ 0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50 };

    // Using range-and-a-half equal:
    bool b = equal(v1.begin(), v1.end(), v2.begin());
    cout << "v1 and v2 are equal: "
        << b << endl; // true, as expected

    b = equal(v1.begin(), v1.end(), v3.begin());
    cout << "v1 and v3 are equal: "
        << b << endl; // true, surprisingly

                      // Using dual-range equal:
    b = equal(v1.begin(), v1.end(), v3.begin(), v3.end());
    cout << "v1 and v3 are equal with dual-range overload: "
        << b << endl; // false

    return 0;
}

不匹配算法示例

#include <vector>
#include <list>
#include <algorithm>
#include <iostream>
#include <string>
#include <utility>

using namespace std;

// Return whether first element is twice the second
// Note that this is not a symmetric, reflexive and transitive equivalence.
// mismatch and equal accept such predicates, but is_permutation does not.
bool twice(int elem1, int elem2)
{
    return elem1 == elem2 * 2;
}

void PrintResult(const string& msg, const pair<vector<int>::iterator, vector<int>::iterator>& result,
    const vector<int>& left, const vector<int>& right)
{
    // If either iterator stops before reaching the end of its container,
    // it means a mismatch was detected.
    if (result.first != left.end() || result.second != right.end())
    {
        string leftpos(result.first == left.end() ? "end" : to_string(*result.first));
        string rightpos(result.second == right.end() ? "end" : to_string(*result.second));
        cout << msg << "mismatch. Left iterator at " << leftpos
            << " right iterator at " << rightpos << endl;
    }
    else
    {
        cout << msg << " match." << endl;
    }
}


int main()
{

    vector<int> vec_1{ 0, 5, 10, 15, 20, 25 };
    vector<int> vec_2{ 0, 5, 10, 15, 20, 25, 30 };

    // Testing different length vectors for mismatch (C++03)
    auto match_vecs = mismatch(vec_1.begin(), vec_1.end(), vec_2.begin());
    bool is_mismatch = match_vecs.first != vec_1.end();
    cout << "C++03: vec_1 and vec_2 are a mismatch: " << boolalpha << is_mismatch << endl;

    // Using dual-range overloads:

    // Testing different length vectors for mismatch (C++14)
    match_vecs = mismatch(vec_1.begin(), vec_1.end(), vec_2.begin(), vec_2.end());
    PrintResult("C++14: vec_1 and vec_2: ", match_vecs, vec_1, vec_2);

    // Identify point of mismatch between vec_1 and modified vec_2. 
    vec_2[3] = 42;
    match_vecs = mismatch(vec_1.begin(), vec_1.end(), vec_2.begin(), vec_2.end());
    PrintResult("C++14 vec_1 v. vec_2 modified: ", match_vecs, vec_1, vec_2);

    // Test vec_3 and vec_4 for mismatch under the binary predicate twice (C++14)  
    vector<int> vec_3{ 10, 20, 30, 40, 50, 60 };
    vector<int> vec_4{ 5, 10, 15, 20, 25, 30 };
    match_vecs = mismatch(vec_3.begin(), vec_3.end(), vec_4.begin(), vec_4.end(), twice);
    PrintResult("vec_3 v. vec_4 with pred: ", match_vecs, vec_3, vec_4);

    vec_4[5] = 31;
    match_vecs = mismatch(vec_3.begin(), vec_3.end(), vec_4.begin(), vec_4.end(), twice);
    PrintResult("vec_3 v. modified vec_4 with pred: ", match_vecs, vec_3, vec_4);

    // Compare a vector<int> to a list<int>
    list<int> list_1{ 0, 5, 10, 15, 20, 25 };
    auto match_vec_list = mismatch(vec_1.begin(), vec_1.end(), list_1.begin(), list_1.end());
    is_mismatch = match_vec_list.first != vec_1.end() || match_vec_list.second != list_1.end();
    cout << "vec_1 and list_1 are a mismatch: " << boolalpha << is_mismatch << endl;

    char c;
    cout << "Press a key" << endl;
    cin >> c;

}

字典排序比较示例

// alg_lex_comp.cpp
// compile with: /EHsc
#include <vector>
#include <list>
#include <algorithm>
#include <iostream>

// Return whether second element is twice the first
bool twice(int elem1, int elem2)
{
    return 2 * elem1 < elem2;
}

int main()
{
    using namespace std;
    vector <int> v1, v2;
    list <int> L1;
    vector <int>::iterator Iter1, Iter2;
    list <int>::iterator L1_Iter, L1_inIter;

    int i;
    for (i = 0; i <= 5; i++)
    {
        v1.push_back(5 * i);
    }
    int ii;
    for (ii = 0; ii <= 6; ii++)
    {
        L1.push_back(5 * ii);
    }

    int iii;
    for (iii = 0; iii <= 5; iii++)
    {
        v2.push_back(10 * iii);
    }

    cout << "Vector v1 = ( ";
    for (Iter1 = v1.begin(); Iter1 != v1.end(); Iter1++)
        cout << *Iter1 << " ";
    cout << ")" << endl;

    cout << "List L1 = ( ";
    for (L1_Iter = L1.begin(); L1_Iter != L1.end(); L1_Iter++)
        cout << *L1_Iter << " ";
    cout << ")" << endl;

    cout << "Vector v2 = ( ";
    for (Iter2 = v2.begin(); Iter2 != v2.end(); Iter2++)
        cout << *Iter2 << " ";
    cout << ")" << endl;

    // Self lexicographical_comparison of v1 under identity
    bool result1;
    result1 = lexicographical_compare(v1.begin(), v1.end(),
        v1.begin(), v1.end());
    if (result1)
        cout << "Vector v1 is lexicographically_less than v1." << endl;
    else
        cout << "Vector v1 is not lexicographically_less than v1." << endl;

    // lexicographical_comparison of v1 and L2 under identity
    bool result2;
    result2 = lexicographical_compare(v1.begin(), v1.end(),
        L1.begin(), L1.end());
    if (result2)
        cout << "Vector v1 is lexicographically_less than L1." << endl;
    else
        cout << "Vector v1 is lexicographically_less than L1." << endl;

    bool result3;
    result3 = lexicographical_compare(v1.begin(), v1.end(),
        v2.begin(), v2.end(), twice);
    if (result3)
        cout << "Vector v1 is lexicographically_less than v2 "
        << "under twice." << endl;
    else
        cout << "Vector v1 is not lexicographically_less than v2 "
        << "under twice." << endl;
}


猜你喜欢

转载自blog.csdn.net/timeinsist/article/details/78440864