C++ Algorithm 不改变参数的函数详解

来源:互联网 发布:网络的拓扑结构 编辑:程序博客网 时间:2024/06/15 20:49
//all_of,对first和last指针之间指向的每一个元素做operation判断,范围为空或全部满足返回true,否则返回false
    //bool all_of(InputIterator first, InputIterator last, Function operation);
    /*array<int, 8> text0 = { 1, 2, 3, 4, 5, 6, 7, 8 };
    bool is_in_edge0 = all_of(text0.begin(), text0.end(), [](int i){return i < 9; });
    cout << *text0.begin() << endl;
    cout << is_in_edge0 << endl;*/
    /*int text1[8] = { 1, 2, 3, 4, 5, 6, 7, 8 };
    bool is_in_edge1 = all_of(text1, text1 + 8, [](int i){return i < 9; });
    cout << *text1 << endl;
    cout << is_in_edge1 << endl;*/

    //any_of,对first和last指针之间指向的每一个元素做operation判断,范围为空或至少有一个满足返回true,否则返回false,范围为假报错
    //bool any_of(InputIterator first, InputIterator last, Function operation);
    /*array<int, 8> text3 = { 1, 2, 3, 4, 5, 6, 7, 8 };
    bool is_in_edge3 = any_of(text3.begin(), text3.end(), [](int i){return i < 3; });
    cout << is_in_edge3 << endl;*/

    //none_of,对first和last指针之间指向的每一个元素做operation判断,范围为空或全部不满足返回true,否则返回false,范围为假报错
    //bool none_of(InputIterator first, InputIterator last, Function operation);
    /*array<int, 8> text4 = { 1, 2, 3, 4, 5, 6, 7, 8 };
    bool is_in_edge4 = none_of(text4.begin(), text4.end(), [](int i){return i < 0; });
    cout << is_in_edge4 << endl;*/

    //for_each,对first和last指针之间指向的每一个元素做operation操作,如果范围为假,则报错
    //for_each(InputIterator first, InputIterator last, Function operation);
    /*array<int, 8> text5 = { 1, 2, 3, 4, 5, 6, 7, 8 };
    for_each(text5.begin(), text5.end(), [](int i){i++; cout << i << " "; });*/

    //find,对first和last指针之间指向的每一个元素,返回第一个和val值相等的元素指针,如果没有相等的,则返回
    //last(如果last没有指定值,会报错),如果范围为假,则报错
    //InputIterator find (InputIterator first, InputIterator last, const T& val);
    /*array<int, 8> text6 = { 1, 2, 3, 4, 5, 6, 7, 8 };
    auto result6 = find(text6.begin(), text6.end(), 1);
    cout << *result6 << endl;*/

    //find_if,对first和last指针之间指向的每一个元素,返回第一个和operation值匹配的元素指针,如果没有匹配的,则返回
    //last(如果last没有指定值,会报错),如果范围为假,则报错
    //InputIterator find_if (InputIterator first, InputIterator last, Function operation);
    /*array<int, 8> text7 = { 1, 2, 3, 4, 5, 6, 7, 8 };
    auto result7 = find_if(text7.begin(), text7.end() , [](int i){return i > 4; });
    cout << *result7 << endl;*/

    //find_if_not,对first和last指针之间指向的每一个元素,返回第一个和operation值不匹配的元素指针,如果全部匹配,则返回
    //last(如果last没有指定值,会报错),如果范围为假,则报错
    //InputIterator find_if_not (InputIterator first, InputIterator last, Function operation);
    /*array<int, 8> text8 = { 1, 2, 3, 4, 5, 6, 7, 8 };
    auto result8 = find_if_not(text8.begin(), text8.end() , [](int i){return i > 4; });
    cout << *result8 << endl;*/

    //find_end,在[first1, last1)范围内查找[first2, last2)序列,如果有,则返回最后一次序列的第一个元素指针,如果没有则返回
    //last1(如果last1没有指定值,会报错),如果范围为假,会报错
    //ForwardIterator1 find_end(ForwardIterator1 first1, ForwardIterator1 last1,
    //    ForwardIterator2 first2, ForwardIterator2 last2);
    /*array<int, 8> text9 = { 1, 2, 3, 4, 5, 6, 7, 8 };
    int a[2] = { 4, 5 };
    auto result9 = find_end(text9.begin(), text9.end(), a, a + 2);
    cout << *result9 << endl;*/

    //find_end,在[first1, last1)范围内查找满足operation操作的[first2, last2)序列,如果有,则返回最后一次序列的第一个元素指针,如果没有则返回
    //last1(如果last1没有指定值,会报错),如果范围为假,会报错
    //ForwardIterator1 find_end(ForwardIterator1 first1, ForwardIterator1 last1,
    //    ForwardIterator2 first2, ForwardIterator2 last2, Function operation);
    /*array<int, 8> text10 = { 1, 2, 3, 4, 5, 6, 7, 8 };
    int a[2] = { 4, 5 };
    auto result10 = find_end(text10.begin(), text10.end(), a, a + 2, [](int i, int j){return i + 1 == j; });
    cout << *result10 << endl;*/

    //find_first_of,在[first1, last1)范围内查找满足[first2, last2)序列中某一个元素的元素,如果有,则返回元素的指针,如果没有则返回
    //last1(如果last1没有指定值,会报错),如果范围为假,会报错
    //InputIterator find_first_of(InputIterator first1, InputIterator last1,
    //    ForwardIterator first2, ForwardIterator last2);
    /*array<int, 8> text11 = { 1, 2, 3, 4, 5, 6, 7, 8 };
    int a[3] = { 10, 5 };
    auto result11 = find_first_of(text11.begin(), text11.end(), a, a + 2);
    cout << *result11 << endl;*/

    //find_first_of,在[first1, last1)范围内查找和[first2, last2)序列中某一个元素满足operation操作的元素,如果有,则返回元素的指针,如果没有则返回
    //last1(如果last1没有指定值,会报错),如果范围为假,会报错
    //InputIterator find_first_of(InputIterator first1, InputIterator last1,
    //    ForwardIterator first2, ForwardIterator last2, Function operation);
    /*array<int, 8> text11 = { 1, 2, 3, 4, 5, 6, 7, 8 };
    int a[3] = { 10, 5 };
    auto result11 = find_first_of(text11.begin(), text11.end(), a, a + 2, [](int i, int j){return i == j + 1; });
    cout << *result11 << endl;*/

    //adjacent_find,在[first, last)范围内查找两个==的或满足operation操作的相邻元素, 如果找到则返回第一个元素的指针,如果没有则返回last
    //ForwardIterator adjacent_find(ForwardIterator first, ForwardIterator last);
    /*int byc0[8] = { 1, 2, 3, 4, 5, 6, 5, 8 };
    int* result12 = adjacent_find(byc0, byc0 + 8, [](int i, int j){return i == j + 1; });
    cout << *result12 << endl;*/

    //count,在[first, last)查找和val值相同的元素的数量
    //count(InputIterator first, InputIterator last, const T& val);
    //count_if,在[first, last)查找满足pred的元素的数量
    //count_if(InputIterator first, InputIterator last, UnaryPredicate pred);
    /*int byc1[8] = { 1, 2, 3, 4, 5, 6, 5, 8 };
    int result13 = count_if(byc1, byc1 + 8, [](int i){return i < 5; });
    cout << result13 << endl;*/

    //mismatch,比较[first1, last1)序列和从first2开始的序列中的元素,如果有不相同的或不满足pred的元素,返回这两个元素的指针
    //pair<InputIterator1, InputIterator2> mismatch(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2);
    //pair<InputIterator1, InputIterator2> mismatch(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, BinaryPredicate pred);
    /*array<int, 8> text12 = { 1, 2, 3, 4, 5, 6, 7, 8 };
    int byc3[8] = { 1, 2, 3, 4, 9, 6, 7, 8 };
    auto result14 = mismatch(text12.begin(), text12.end(), byc3);
    cout << *result14.first << *result14.second << endl;*/

    //equal,比较[first1, last1)序列和从first2开始的序列中的元素,如果范围为空或全部相同的或全部满足pred,返回true,否则返回false
    //bool equal(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2);
    //bool equal(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, BinaryPredicate pred);
    /*array<int, 8> text13 = { 1, 2, 3, 4, 5, 6, 7, 8 };
    int byc4[8] = { 1, 2, 3, 4, 5, 6, 7, 8 };
    auto result15 = equal(text13.begin(), text13.end(), byc4);
    cout << result15 << endl;*/

    //is_permutation,比较[first1, last1)序列和从first2开始的序列中的元素,如果范围为空或全部相同的或全部满足pred即使顺序不同,也返回true,否则返回false
    //bool is_permutation(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2);
    //bool is_permutation(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, BinaryPredicate pred);
    //第二个方法尽量少用,内部算法未知,可能会出错
    /*array<int, 8> text14 = { 1, 2, 3, 4, 5, 6, 7, 8 };
    int byc5[8] = { 8, 7, 6, 5, 4, 3, 2, 9 };
    auto result16 = is_permutation(text14.begin(), text14.end(), byc5 );
    cout << result16 << endl;*/
    

    //search,和find_end极为相似,在[first1, last1)范围内查找相等或满足operation操作的[first2, last2)序列,如果有,则返回第一次序列的第一个元素指针,
    //如果没有则返回last1
    //ForwardIterator1 search (ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2);
    //ForwardIterator1 search (ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2, Function operation);
    /*array<int, 8> text15 = { 1, 2, 3, 4, 5, 6, 7, 8 };
    int a[4] = { 4, 5 };
    auto result17 = search(text15.begin(), text15.end(), a, a + 2);
    cout << *result17 << endl;*/
        
    
    //search_n,和adjacent_find极为相似,在[first, last)范围内查找连续有count个val值的序列,如果有则返回第一个指针,没有返回last
    //ForwardIterator search_n(ForwardIterator first, ForwardIterator last, Size count, const T& val);
    //ForwardIterator search_n(ForwardIterator first, ForwardIterator last, Size count, const T& val, BinaryPredicate pred);
    /*array<int, 8> text16 = { 1, 2, 2, 2, 4, 2, 5, 7 };
    auto result18 = search_n(text16.begin(), text16.end(), 3, 2);
    cout << *result18 << endl;*/

原创粉丝点击