STL-Lesson004_001

来源:互联网 发布:python绘图 编辑:程序博客网 时间:2024/06/05 15:26
#ifndef _MYALGORITHM_H_#define _MYALGORITHM_H_#include <iterator>#include <tuple>namespace PoEdu{    // all_of    // Test condition on all elements in range (function template )    template<typename InputIterator, typename UnaryPredicate>    bool all_of(InputIterator first, InputIterator last, UnaryPredicate pred)    {        bool ret = true;        while (first != last)        {            if (!pred(*first))            {                ret = false;                break;            }            ++first;        }        return ret;    }    // any_of    // Test if any element in range fulfills condition (function template )    template<typename InputIterator, typename UnaryPredicate>    bool any_of(InputIterator first, InputIterator last, UnaryPredicate pred)    {        bool ret = false;        while (first != last)        {            if (pred(*first))            {                ret = true;                break;            }            ++first;        }        return ret;    }    // none_of    // Test if no elements fulfill condition (function template )    template<typename InputIterator, typename UnaryPredicate>    bool none_of(InputIterator first, InputIterator last, UnaryPredicate pred)    {        bool ret = true;        while (first != last)        {            if (pred(*first))            {                ret = false;                break;            }            ++first;        }        return ret;    }    // for_each    // Apply function to range (function template )    template<typename InputIterator, typename Function>    Function for_each(InputIterator first, InputIterator last, Function fn)    {        while (first != last)        {            fn(*first);            ++first;        }        return fn;    }    // find    // Find value in range (function template )    template<typename InputIterator, typename T>    InputIterator find(InputIterator first, InputIterator last, const T& val)    {        InputIterator ret = last;        while (first != last)        {            if (*first == val)            {                ret = first;                break;            }            ++first;        }        return ret;    }    // find_if    // Find element in range (function template )    template<typename InputIterator, typename UnaryPredicate>    InputIterator find_if(InputIterator first, InputIterator last, UnaryPredicate pred)    {        InputIterator ret = last;        while (first != last)        {            if (pred(*first))            {                ret = first;                break;            }            ++first;        }        return ret;    }    // find_if_not    // Find element in range(negative condition) (function template)    template<typename InputIterator, typename UnaryPredicate>    InputIterator find_if_not(InputIterator first, InputIterator last, UnaryPredicate pred)    {        InputIterator ret = last;        while (first != last)        {            if (!pred(*first))            {                ret = first;                break;            }            ++first;        }        return ret;    }    // find_end    // Find last subsequence in range(function template)    template<typename ForwardIterator1, typename ForwardIterator2>    ForwardIterator1 find_end(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2)    {        if (first2 == last2)        {            return last1;        }        ForwardIterator1 ret = last1;        while (first1 != last1)        {            ForwardIterator1 it1 = first1;            ForwardIterator2 it2 = first2;            while (*it1 == *it2)            {                ++it1;                ++it2;                if (it2 == last2)                {                    ret = first1;                    break;                }                if (it1 == last1)                {                    return ret;                }            }            ++first1;        }        return ret;    }    template <typename ForwardIterator1, typename ForwardIterator2, typename BinaryPredicate>    ForwardIterator1 find_end(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred)    {        if (first2 == last2)        {            return last1;        }        ForwardIterator1 ret = last1;        while (first1 != last1)        {            ForwardIterator1 it1 = first1;            ForwardIterator2 it2 = first2;            while (pred(*it1, *it2))            {                ++it1;                ++it2;                if (it2 == last2)                {                    ret = first1;                    break;                }                if (it1 == last1)                {                    return ret;                }            }            ++first1;        }        return ret;    }    // find_first_of    // Find element from set in range(function template)    template<typename InputIterator, typename ForwardIterator>    InputIterator find_first_of(InputIterator first1, InputIterator last1, ForwardIterator first2, ForwardIterator last2)    {        while (first1 != last1)        {            for (ForwardIterator it = first2; it != last2; ++it)            {                if (*it == *first1)                {                    return first1;                }            }            ++first1;        }        return last1;    }    template <typename InputIterator, typename ForwardIterator, typename BinaryPredicate>    InputIterator find_first_of(InputIterator first1, InputIterator last1, ForwardIterator first2, ForwardIterator last2, BinaryPredicate pred)    {        while (first1 != last1)        {            for (ForwardIterator it = first2; it != last2; ++it)            {                if (pred(*it, *first1))                {                    return first1;                }            }            ++first1;        }        return last1;    }    // adjacent_find    // Find equal adjacent elements in range(function template)    template <typename ForwardIterator>    ForwardIterator adjacent_find(ForwardIterator first, ForwardIterator last)    {        ForwardIterator ret = last;        if (first != last)        {            ForwardIterator next = first;            ++next;            while (next != last)            {                if (*first == *next)                {                    ret = first;                    break;                }                ++first;                ++next;            }        }        return ret;    }    template <typename ForwardIterator, typename BinaryPredicate>    ForwardIterator adjacent_find(ForwardIterator first, ForwardIterator last, BinaryPredicate pred)    {        ForwardIterator ret = last;        if (first != last)        {            ForwardIterator next = first;            ++next;            while (next != last)            {                if (pred(*first, *next))                {                    ret = first;                    break;                }                ++first;                ++next;            }        }        return ret;    }    // count    // Count appearances of value in range(function template)    template <typename InputIterator, typename T>    typename std::iterator_traits<InputIterator>::difference_type        count(InputIterator first, InputIterator last, const T& val)    {        typename std::iterator_traits<InputIterator>::difference_type ret = 0;        while (first != last)        {            if (*first == val)            {                ++ret;            }            ++first;        }        return ret;    }    // count_if    // Return number of elements in range satisfying condition(function template)    template <typename InputIterator, typename UnaryPredicate>    typename std::iterator_traits<InputIterator>::difference_type        count_if(InputIterator first, InputIterator last, UnaryPredicate pred)    {        typename std::iterator_traits<InputIterator>::difference_type ret = 0;        while (first != last)        {            if (pred(*first))            {                ++ret;            }            ++first;        }        return ret;    }    // mismatch    // Return first position where two ranges differ(function template)    template <typename InputIterator1, typename InputIterator2>    std::pair<InputIterator1, InputIterator2> mismatch(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2)    {        while ((first1 != last1) && (*first1 == *first2))        {            ++first1;            ++first2;        }        return std::make_pair(first1, first2);    }    template <typename InputIterator1, typename InputIterator2, typename BinaryPredicate>    std::pair<InputIterator1, InputIterator2> mismatch(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, BinaryPredicate pred)    {        while ((first1 != last1) && (pred(*first1, *first2)))        {            ++first1;            ++first2;        }        return std::make_pair(first1, first2);    }    // equal    // Test whether the elements in two ranges are equal(function template)    template <typename InputIterator1, typename InputIterator2>    bool equal(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2)    {        bool ret = true;        while (first1 != last1)        {            if (!(*first1 == *first2))            {                ret = false;            }            ++first1;            ++first2;        }        return ret;    }    template <typename InputIterator1, typename InputIterator2, typename BinaryPredicate>    bool equal(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, BinaryPredicate pred)    {        bool ret = true;        while (first1 != last1)        {            if (!(pred(*first1, *first2)))            {                ret = false;            }            ++first1;            ++first2;        }        return ret;    }    // is_permutation    // Test whether range is permutation of another(function template)    template <typename InputIterator1, typename InputIterator2>    bool is_permutation(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2)    {        std::tie(first1, first2) = PoEdu::mismatch(first1, last1, first2);        if (first1 == last1) return true;        InputIterator2 last2 = first2; std::advance(last2, std::distance(first1, last1));        for (InputIterator1 it1 = first1; it1 != last1; ++it1)        {            if (PoEdu::find(first1, it1, *it1) == it1)            {                auto n = PoEdu::count(first2, last2, *it1);                if (n == 0 || PoEdu::count(it1, last1, *it1) != n)                    return false;            }        }        return true;    }    template <typename InputIterator1, typename InputIterator2, typename BinaryPredicate>    bool is_permutation(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, BinaryPredicate pred)    {        std::tie(first1, first2) = PoEdu::mismatch(first1, last1, first2);        if (first1 == last1)        {            return true;        }        InputIterator2 last2 = first2; std::advance(last2, std::distance(first1, last1));        for (InputIterator1 it1 = first1; it1 != last1; ++it1)        {            if (find(first1, it1, *it1) == it1)            {                auto n = count(first2, last2, *it1);                if (n == 0 || count(it1, last1, *it1) != n)                {                    return false;                }            }        }        return true;    }    // search    // Search range for subsequence(function template)    template<typename ForwardIterator1, typename ForwardIterator2>    ForwardIterator1 search(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2)    {        if (first2 == last2)        {            return first1;        }        while (first1 != last1)        {            ForwardIterator1 it1 = first1;            ForwardIterator2 it2 = first2;            while (*it1 == *it2)            {                ++it1;                ++it2;                if (it2 == last2)                {                    return first1;                }                if (it1 == last1)                {                    return last1;                }            }            ++first1;        }        return last1;    }    template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>    ForwardIterator1 search(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred)    {        if (first2 == last2)        {            return first1;        }        while (first1 != last1)        {            ForwardIterator1 it1 = first1;            ForwardIterator2 it2 = first2;            while (pred(*it1, *it2))            {                ++it1;                ++it2;                if (it2 == last2)                {                    return first1;                }                if (it1 == last1)                {                    return last1;                }            }            ++first1;        }        return last1;    }    // search_n    // Search range for elements(function template)    template<typename ForwardIterator, typename Size, typename T>    ForwardIterator search_n(ForwardIterator first, ForwardIterator last, Size count, const T& val)    {        ForwardIterator it, limit;        Size i;        limit = first;        std::advance(limit, std::distance(first, last) - count);        while (first != limit)        {            it = first; i = 0;            while (*it == val)            {                ++it;                if (++i == count)                {                    return first;                }            }            ++first;        }        return last;    }    template <typename ForwardIterator, typename Size, typename T, typename BinaryPredicate>    ForwardIterator search_n(ForwardIterator first, ForwardIterator last, Size count, const T& val, BinaryPredicate pred)    {        ForwardIterator it, limit;        Size i;        limit = first;        std::advance(limit, std::distance(first, last) - count);        while (first != limit)        {            it = first; i = 0;            while (pred(*it, val))            {                ++it;                if (++i == count)                {                    return first;                }            }            ++first;        }        return last;    }}#endif//!_MYALGORITHM_H_

Test

#include <iostream>#include <array>#include <vector>#include <cctype>#include "myalgorithm.h"#define _TEST_ALL_OF_//#define _TEST_ANY_OF_//#define _TEST_NONE_OF_//#define _TEST_FIND_//#define _TEST_FIND_IF_//#define _TEST_FIND_IF_NOT_//#define _TEST_FIND_END_//#define _TEST_FIND_FIRST_OF_//#define _TEST_ADJACENT_FIND_//#define _TEST_COUNT_//#define _TEST_COUNT_IF_//#define _TEST_MISMATCH_//#define _TEST_EQUAL_//#define _TEST_IS_PERMUTATION_//#define _TEST_SEARCH_//#define _TEST_SEARCH_N_#ifdef _TEST_FOR_EACH_void myfunction(int i){    std::cout << ' ' << i;}#endif//!_TEST_FOR_EACH_#ifdef _TEST_FOR_EACH_struct myclass{    void operator() (int i)    {        std::cout << ' ' << i;    }} myobject;#endif//!_TEST_FOR_EACH_#ifdef _TEST_FIND_IF_bool IsOdd(int i){    return ((i % 2) == 1);}#endif//!_TEST_FIND_IF_#ifdef _TEST_FIND_END_bool myfunction(int i, int j){    return (i == j);}#endif//!_TEST_FIND_END_#ifdef _TEST_FIND_FIRST_OF_bool comp_case_insensitive(char c1, char c2){    return (std::tolower(c1) == std::tolower(c2));}#endif//!_TEST_FIND_FIRST_OF_#ifdef _TEST_ADJACENT_FIND_bool myfunction (int i, int j){    return i == j;}#endif//!_TEST_ADJACENT_FIND_#ifdef _TEST_COUNT_IF_bool IsOdd(int i){    return ((i % 2) == 1);}#endif//!_TEST_COUNT_IF_#ifdef _TEST_MISMATCH_bool mypredicate(int i, int j){    return (i == j);}#endif//!_TEST_MISMATCH_#ifdef _TEST_EQUAL_bool mypredicate(int i, int j){    return  (i == j);}#endif//!_TEST_EQUAL_#ifdef _TEST_SEARCH_bool mypredicate(int i, int j){    return (i == j);}#endif//!_TEST_SEARCH_#ifdef _TEST_SEARCH_N_bool mypredicate(int i, int j){    return (i == j);}#endif//!_TEST_SEARCH_N_int main(){#ifdef _TEST_ALL_OF_    std::cout << "test all_of..." << std::endl;    std::array<int, 8> foo1 = { 3,5,7,11,13,17,19,23 };    if (PoEdu::all_of(foo1.begin(), foo1.end(), [](int i) {return i % 2; }))    {        std::cout << "All the elements of foo1 are odd numbers.\n";    }    else    {        std::cout << "Not all the elements of foo1 are odd numbers.\n";    }    std::array<int, 8> foo2 = { 3,5,7,12,13,17,19,23 };    if (PoEdu::all_of(foo2.begin(), foo2.end(), [](int i) {return i % 2; }))    {        std::cout << "All the elements of foo2 are odd numbers.\n";    }    else    {        std::cout << "Not all the elements of foo2 are odd numbers.\n";    }    std::cout << std::endl;#endif//!_TEST_ALL_OF_#ifdef _TEST_ANY_OF_    std::cout << "test any_of..." << std::endl;    std::array<int, 7> foo1 = { 0,1,-1,3,-3,5,-5 };    if (PoEdu::any_of(foo1.begin(), foo1.end(), [](int i) {return i < 0; }))    {        std::cout << "There are negative elements of foo1 in the range.\n";    }    else    {        std::cout << "There are not negative elements of foo1 in the range.\n";    }    std::array<int, 7> foo2 = { 0,1,2,3,4,5,6 };    if (PoEdu::any_of(foo2.begin(), foo2.end(), [](int i) {return i < 0; }))    {        std::cout << "There are negative elements of foo2 in the range.\n";    }    else    {        std::cout << "There are not negative elements of foo2 in the range.\n";    }#endif//!_TEST_ANY_OF_#ifdef _TEST_NONE_OF_    std::array<int, 8> foo1 = { 1,2,4,8,16,32,64,128 };    if (PoEdu::none_of(foo1.begin(), foo1.end(), [](int i) {return i < 0; }))    {        std::cout << "There are no negative elements of foo1 in the range.\n";    }    else    {        std::cout << "There are not no negative elements of foo1 in the range.\n";    }    std::array<int, 8> foo2 = { 1,2,4,8,-16,32,64,128 };    if (PoEdu::none_of(foo2.begin(), foo2.end(), [](int i) {return i < 0; }))    {        std::cout << "There are no negative elements of foo2 in the range.\n";    }    else    {        std::cout << "There are negative elements of foo2 in the range.\n";    }#endif//!_TEST_NONE_OF_#ifdef _TEST_FOR_EACH_    std::cout << "test for_each..." << std::endl;    std::vector<int> myvector;    myvector.push_back(10);    myvector.push_back(20);    myvector.push_back(30);    std::cout << "myvector contains:";    PoEdu::for_each(myvector.begin(), myvector.end(), myfunction);    std::cout << '\n';    std::cout << "myvector contains:";    PoEdu::for_each(myvector.begin(), myvector.end(), myobject);    std::cout << '\n';    std::cout << std::endl;#endif//!_TEST_FOR_EACH_#ifdef _TEST_FIND_    std::cout << "test find..." << std::endl;    // using std::find with array and pointer:    int myints[] = { 10, 20, 30, 40 };    int * p;    p = PoEdu::find(myints, myints + 4, 30);    if (p != myints + 4)    {        std::cout << "Element found in myints: " << *p << '\n';    }    else    {        std::cout << "Element not found in myints\n";    }    // using std::find with vector and iterator:    std::vector<int> myvector(myints, myints + 4);    std::vector<int>::iterator it;    it = PoEdu::find(myvector.begin(), myvector.end(), 20);    if (it != myvector.end())    {        std::cout << "Element found in myvector: " << *it << '\n';    }    else    {        std::cout << "Element not found in myvector\n";    }    std::cout << std::endl;#endif//!_TEST_FIND_#ifdef _TEST_FIND_IF_    std::cout << "test find_if..." << std::endl;    std::vector<int> myvector;    myvector.push_back(10);    myvector.push_back(25);    myvector.push_back(40);    myvector.push_back(55);    std::vector<int>::iterator it = PoEdu::find_if(myvector.begin(), myvector.end(), IsOdd);    std::cout << "The first odd value is " << *it << '\n';#endif//!_TEST_FIND_IF_#ifdef _TEST_FIND_IF_NOT_    std::cout << "test find_if_not..." << std::endl;    std::array<int, 5> foo = { 1,2,3,4,5 };    std::array<int, 5>::iterator it = PoEdu::find_if_not(foo.begin(), foo.end(), [](int i) {return i % 2; });    std::cout << "The first even value is " << *it << '\n';#endif//!_TEST_FIND_IF_NOT_#ifdef _TEST_FIND_END_    std::cout << "test find_end..." << std::endl;    int myints[] = { 1,2,3,4,5,1,2,3,4,5 };    std::vector<int> haystack(myints, myints + 10);    int needle1[] = { 1,2,3 };    // using default comparison:    std::vector<int>::iterator it;    it = PoEdu::find_end(haystack.begin(), haystack.end(), needle1, needle1 + 3);    if (it != haystack.end())    {        std::cout << "needle1 last found at position " << (it - haystack.begin()) << '\n';    }    else    {        std::cout << "needle1 last not found" << '\n';    }    int needle2[] = { 4,5,1 };    // using predicate comparison:    it = PoEdu::find_end(haystack.begin(), haystack.end(), needle2, needle2 + 3, myfunction);    if (it != haystack.end())    {        std::cout << "needle2 last found at position " << (it - haystack.begin()) << '\n';    }#endif//!_TEST_FIND_END_#ifdef _TEST_FIND_FIRST_OF_    std::cout << "test find_first_of..." << std::endl;    int mychars[] = { 'a','b','c','A','B','C' };    std::vector<char> haystack(mychars, mychars + 6);    std::vector<char>::iterator it;    int needle[] = { 'A','B','C' };    // using default comparison:    it = PoEdu::find_first_of(haystack.begin(), haystack.end(), needle, needle + 3);    if (it != haystack.end())    {        std::cout << "The first match is: " << *it << '\n';    }    // using predicate comparison:    it = PoEdu::find_first_of(haystack.begin(), haystack.end(), needle, needle + 3, comp_case_insensitive);    if (it != haystack.end())    {        std::cout << "The first match is: " << *it << '\n';    }#endif#ifdef _TEST_ADJACENT_FIND_    std::cout << "test adjacent_find..." << std::endl;    int myints[] = { 5,20,5,30,30,20,10,10,20 };    std::vector<int> myvector(myints, myints + 8);    std::vector<int>::iterator it;    // using default comparison:    it = PoEdu::adjacent_find(myvector.begin(), myvector.end());    if (it != myvector.end())    {        std::cout << "the first pair of repeated elements are: " << *it << '\n';    }    //using predicate comparison:    it = PoEdu::adjacent_find(++it, myvector.end(), myfunction);    if (it != myvector.end())    {        std::cout << "the second pair of repeated elements are: " << *it << '\n';    }#endif//!_TEST_ADJACENT_FIND_#ifdef _TEST_COUNT_    std::cout << "test count..." << std::endl;    int myints[] = { 10,20,30,30,20,10,10,20 };   // 8 elements    int mycount = PoEdu::count(myints, myints + 8, 10);    std::cout << "10 appears " << mycount << " times.\n";    // counting elements in container:    std::vector<int> myvector(myints, myints + 8);    mycount = PoEdu::count(myvector.begin(), myvector.end(), 20);    std::cout << "20 appears " << mycount << " times.\n";#endif//!_TEST_COUNT_#ifdef _TEST_COUNT_IF_    std::cout << "test count_if..." << std::endl;    std::vector<int> myvector;    for (int i = 1; i < 10; i++)    {        myvector.push_back(i);// myvector: 1 2 3 4 5 6 7 8 9    }    int mycount = PoEdu::count_if(myvector.begin(), myvector.end(), IsOdd);    std::cout << "myvector contains " << mycount << " odd values.\n";#endif//!_TEST_COUNT_IF_#ifdef _TEST_MISMATCH_    std::cout << "test mismatch..." << std::endl;    std::vector<int> myvector;    for (int i = 1; i < 6; i++)    {        myvector.push_back(i * 10);// myvector: 10 20 30 40 50    }    int myints[] = { 10,20,80,320,1024 };                //   myints: 10 20 80 320 1024    std::pair<std::vector<int>::iterator, int*> mypair;    // using default comparison:    mypair = PoEdu::mismatch(myvector.begin(), myvector.end(), myints);    std::cout << "First mismatching elements: " << *mypair.first;    std::cout << " and " << *mypair.second << '\n';    ++mypair.first;    ++mypair.second;    // using predicate comparison:    mypair = PoEdu::mismatch(mypair.first, myvector.end(), mypair.second, mypredicate);    std::cout << "Second mismatching elements: " << *mypair.first;    std::cout << " and " << *mypair.second << '\n';#endif//!_TEST_MISMATCH_#ifdef _TEST_EQUAL_    std::cout << "test equal..." << std::endl;    int myints[] = { 20,40,60,80,100 };               //   myints: 20 40 60 80 100    std::vector<int>myvector(myints, myints + 5);     // myvector: 20 40 60 80 100                                                      // using default comparison:    if (PoEdu::equal(myvector.begin(), myvector.end(), myints))    {        std::cout << "The contents of both sequences are equal.\n";    }    else    {        std::cout << "The contents of both sequences differ.\n";    }    myvector[3] = 81;                                 // myvector: 20 40 60 81 100                                                      // using predicate comparison:    if (PoEdu::equal(myvector.begin(), myvector.end(), myints, mypredicate))    {        std::cout << "The contents of both sequences are equal.\n";    }    else    {        std::cout << "The contents of both sequences differ.\n";    }#endif//!_TEST_EQUAL_#ifdef _TEST_IS_PERMUTATION_    std::cout << "test is_permutation..." << std::endl;    std::array<int, 5> foo = { 1,2,3,4,5 };    std::array<int, 5> bar = { 3,1,4,5,2 };    if (PoEdu::is_permutation(foo.begin(), foo.end(), bar.begin()))    {        std::cout << "foo and bar contain the same elements.\n";    }#endif//!_TEST_IS_PERMUTATION_#ifdef _TEST_SEARCH_    std::cout << "test search..." << std::endl;    std::vector<int> haystack;    // set some values:        haystack: 10 20 30 40 50 60 70 80 90    for (int i = 1; i < 10; i++)    {        haystack.push_back(i * 10);    }    // using default comparison:    int needle1[] = { 40,50,60,70 };    std::vector<int>::iterator it;    it = PoEdu::search(haystack.begin(), haystack.end(), needle1, needle1 + 4);    if (it != haystack.end())    {        std::cout << "needle1 found at position " << (it - haystack.begin()) << '\n';    }    else    {        std::cout << "needle1 not found\n";    }    // using predicate comparison:    int needle2[] = { 20,30,50 };    it = PoEdu::search(haystack.begin(), haystack.end(), needle2, needle2 + 3, mypredicate);    if (it != haystack.end())    {        std::cout << "needle2 found at position " << (it - haystack.begin()) << '\n';    }    else    {        std::cout << "needle2 not found\n";    }#endif//!_TEST_SEARCH_#ifdef _TEST_SEARCH_N_    std::cout << "test search_n..." << std::endl;    int myints[] = { 10,20,30,30,20,10,10,20 };    std::vector<int> myvector(myints, myints + 8);    std::vector<int>::iterator it;    // using default comparison:    it = PoEdu::search_n(myvector.begin(), myvector.end(), 2, 30);    if (it != myvector.end())    {        std::cout << "two 30s found at position " << (it - myvector.begin()) << '\n';    }    else    {        std::cout << "match not found\n";    }    // using predicate comparison:    it = PoEdu::search_n(myvector.begin(), myvector.end(), 2, 10, mypredicate);    if (it != myvector.end())    {        std::cout << "two 10s found at position " << int(it - myvector.begin()) << '\n';    }    else    {        std::cout << "match not found\n";    }#endif//!_TEST_SEARCH_N_    return 0;}
原创粉丝点击