Standard Library Algorithm Implementation 1: Standard Library Algorithm Implementation

Source: Internet
Author: User

Standard Library Algorithm Implementation 1: Standard Library Algorithm Implementation
1. max (x, y)
2. find (B, e, t)
3. find_if (B, e, p)
4. search (B, e, b2, e2)
5. remove (B, e, t)
6. copy (B, e, d)
7. remove_copy (B, e, d, t)
8. remove_copy_if (B, e, d, p)
9. replace (B, e, x, y)
10. swap (x, y)
11. reverse (B, e)
12. binary_search (B, e, x)
13. split (s)
14. equal (B, e, b2)
15. transform (B, e, d, f)
16. partition (B, e, p)
17. accumulate (B, e, t)

1.max(x,y)template <class T>T max(const T& x,const T& y){    return x<y?y:x;}2.find(b,e,t)template <class In,class X>In find(In begin,In end,const X& x){    //In is an iterator or const_iterator    while(begin!=end && *begin!=x)        ++begin;    return begin;}3.find_if(b,e,p)template <class In>In find_if(In begin,In end,p){    while(begin!=end)    {        if(p(*begin))        {            return begin;        }        ++begin;    }}4.search(b,e,b2,e2)template <class In>In search(In b,In e,In b2,In e2){    int k=0;    In start;    while(b2!=e2)    {        while(b!=e)        {            if(k==0 && *b==*b2)            {                start=b;                ++b;                ++k;                break;            }            else if(*b==*b2)            {                ++b;                break;            }            ++b;        }        ++b2;    }    return start;}5.remove(b,e,t)//put the element that are !=t in front of the containertemplate <class X>void sw(X& x,X& y){    X temp=x;    x=y;    y=temp;}template <class In,class X>In remove(In begin,In end,X& x){    while(begin!=end)    {        if(*begin==x)        {            --end;            while(*end==x)                --end;            sw(*begin,*end);        }        ++begin;    }    return end;}6.copy(b,e,d)template <class In,class Out>Out copy(In begin,In end,Out dest){    while(begin!=end)        *dest++=*begin++;    return dest;}7.remove_copy(b,e,d,t)template <class In,class Out,class X>Out remove_copy(In begin,In end,Out dest,const X& x){    while(begin!=end)    {        if(*begin==x)            *d++=*begin;        ++begin;    }    return dest;}8.remove_copy_if(b,e,d,p)template <class X>bool fun(const X& x){    return *x>10;}template <class In,class Out>Out remove_copy_if(In begin,In end,Out dest,bool fun(const In&)){    while(begin!=end)    {        if(!fun(begin))            *dest++=*begin;        ++begin;    }    return dest;}9.replace(b,e,x,y)template <class In,class X>void replace(In begin,In end,const X& x,const X& y){    while(begin!=end)    {        if(*begin==x)            *begin=y;        ++begin;    }}10.swap(x,y)template <class X>void swap(X& x,X& y){    X temp;    temp=x;    x=y;    y=temp;}11.reversetemplate <class In>void reverse(In begin,In end){    while(begin!=end)    {        --end;        if(begin!=end)        {            swap(*begin++,*end);        }    }}12.binary_searchtemplate <class In,class X>In binary_search(In begin,In end,const X& x){    //the function is return a iterator,and if not find    //we let it return the second arguments(end)    while(begin!=end)    {        In mid=begin+(end-begin)/2;        if(*mid<x)            end=mid;        else if(x<*mid)            begin=mid+1;        else            return mid;    }    return end;}13.split/*bool space(char c){    return isspace(c);}bool not_space(char c){    return !isspace(c);}*/template <class Out>void split(const string& s,Out os){    typedef string::const_iterator iter;    iter i=s.begin();    iter e=s.end();    while(i!=e)    {        i=find_if(i,e,not_space);        iter j=find_if(i,e,space);        if(i!=e)            *os++=string(i,j);        i=j;    }}14.equal(b,e,b2)template <class In>bool equal(In beg,In end,In beg2){    while(beg!=end)    {        if(*beg!=*beg2)        {            return false;        }        ++beg;        ++beg2;    }    return true;}15.transform(b,e,d,f)template <class In,class Out>Out transform(In beg,In end,Out beg2,,bool fun(In)){    while(beg!=end)    {        if(fun(beg))        {            *beg2=*beg;            ++beg;        }        ++beg2;        ++beg;    }}16.partition(b,e,p)/* *b,e is a bothway iterator;if p return true put the elements into the former of the container,else into the later; *return a iterator direct to the first dissatified elements.*/template <class Y>bool fun(Y& x){    return *x<6;}template <class X>void sw(X& x,X& y){    X temp=x;    x=y;    y=temp;}template <class In>In partition(In beg,In end,bool fun(In&)){    while(beg!=end)    {        while(fun(beg))        {            ++beg;            if(beg==end)                return beg;        }        do{            --end;            if(beg==end)                return beg;        }while(!fun(end));        sw(*beg,*end);        ++beg;    }    return beg;}17.accumulate(b,e,t)template <class In,class X>X accumulate(In beg,In end,X x){    while(beg!=end)    {        x+=*beg;        ++beg;    }    return x;}





C ++ standard library Algorithm

Implement it by yourself...

How can we see the source code of the C ++ standard library algorithm?

Install a plug-in called VisualAssist
It can jump directly to the function source code, provided that your function source code is visible.

These are all in STL. Generally, they are in the include directory of your installation directory. There is a file named algorithm that specifically implements STL algorithms.
 

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.