Stl_ algorithm _ Find Algorithm (Find, find_if)

Source: Internet
Author: User

C + + Primer learning.

。。

Simple recording of My Learning process (code-based)

Find, Find_if

/********************** Linear lookup O (n) find (); find_if (); Note: 1. The assumption is that the interval lookup algorithm can be used 2. An associative container (SET,MAP) has an equivalent member function find (); Time Complexity O ( Log (n)) 3.string has equivalent member function find (); **********************/#include <iostream> #include <cstdio> #include <string> #include <vector> #include <set> #include <algorithm> #include <functional> Using namespace STD;/*************************************************************************************STD:: Find Algorithm----------------------------------------- ---------------------------------------------Template <class Inputiterator, class t> Inputiterator find ( Inputiterator First, Inputiterator last, const t& value); Eg:template<class Inputiterator, Class t> Inputiterat Or find (inputiterator first, Inputiterator last, const t& value) {for (; first!=last; first++) if (*first==va    Lue) break;  return first; }**************************************************************************************//***************************************************************** STD::FIND_IF algorithm------------- -------------------------------------------------------------------------Template <class Inputiterator, class Predicate> inputiterator find_if (inputiterator First, inputiterator last, predicate pred); Eg:template<class INP Utiterator, Class predicate> Inputiterator find_if (inputiterator First, inputiterator last, predicate pred) {f    or (; first!=last; first++) if (pred (*first)) break;  return first; }**************************************************************************************/bool IsEven (int i); int    Main () {int myints[] = {10,30,20,40,20,10,30,40};    int * p;    Pointer to array element:p = Find (myints,myints+8,30);    ++p;    cout << "The element following" << *p << Endl; VectoR<int> Myvector (myints,myints+8);    Vector<int>::iterator it;    iterator to vector element:it = find (Myvector.begin (), Myvector.end (), 30);    ++it;    cout << "The element following" << *it << Endl;    Outputs the number Vector<int>::iterator it2 in the second 30 interval of the first---;    It2=find (It,myvector.end (), 30);    while (it!=it2) cout<<*it++<< ""; cout<<endl;/**--------------------------------find_if ()---------------------------------**///Find the first even-numbered it = Find_if (Myvector.begin (), Myvector.end (), IsEven);//function or Function object cout << "The first odd value is" << *it &lt    ;< Endl;    It2 = find_if (Myvector.begin (), Myvector.end (), Not1 (bind2nd (Modulus<int> (), 2))); cout << "The first odd value is" << *it2 << endl;/**--------------------------------associated Container--------------    -------------------**/set<int> S (myvector.begin (), Myvector.begin () +4); cout<< "Complexity is O (log (n)), find *s.find (+): =" <&Lt *s.find << endl;/**---------------------------------string----------------------------------**/string    St ("Angelababy");    String::size_type pos = st.find ("Baby"); if (pos! = string::npos) cout<< "Find it!"    <<endl; else cout<< "not find it!"    <<endl;    pos = St.find ("Baby"); if (pos! = string::npos) cout<< "Find it!"    <<endl; else cout<< "not find it!"    <<endl; return 0;} BOOL IsEven (int i) {return ((i%2) ==0);} /******output:the element Following, the element following, the first odd value I    S. The first odd value is 10 complexity is O (log (n)), find *s.find (+): = it! Find Not find it!******/


Stl_ algorithm _ Find Algorithm (Find, find_if)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.