The STL find_if algorithm is used to find the eligible elements in the container __ algorithm

Source: Internet
Author: User
The STL find_if algorithm is used to find the eligible elements within the containerExamples are as follows:
1. The first way: to save the value to be compared in the constructor of an imitation function.
struct Stableinfor {
        uint16 m_itableid;
}


First write The Imitation function:
Class  Tablecomparefuctor
{public
    :
    
    tablecomparefuctor (const stableinfor tableinfo)
    {
        m_ Tableinfo = Tableinfo;
    }
    
    ~ Tablecomparefuctor ()
    {
        
    }
    
    bool Operator () (const stableinfor  value)
    {
        if (value.m_ Itableid = = M_tableinfo.m_itableid) 
        {return
            true;
        }
        else
        {
            return  false;
        }
    }
    
    Private:
    
    stableinfor  m_tableinfo;



Finally, call the FIND_IF algorithm: include the header file first:
#import "algorithm"

using namespace std;


Std::list<stableinfor>  tableinforlist;

Std::list<stableinfor>::iterator  iter_begin =  tableinforlist.begin ();
    Std::list<stableinfor>::iterator  iter_end = Tableinforlist.end ();
    
    Tablecomparefuctor  Comparefuctor (tableinfo.m_tableinfor);
    
    Std::list<stableinfor>::iterator  iter_find = find_if (Iter_begin, Iter_end, comparefuctor);
    
    if (iter_find!= iter_end) 
    {
        tableinforlist.erase (iter_find);
    }
The example above is to find the element that meets the criteria first and then remove it from the container.



2. The second approach: use of

Binary_function and

bind2nd

If you want to use the blind2nd adapter for your own imitation function, you must allow your own imitation function to inherit from Binary_function.

bind2nd represents the 2nd parameter of the binding, or it can be bind1st to bind the first argument.

The code example is as follows:

struct  tablecomparefuctorwithadapter:public std::binary_function<stableinfor,stableinfor,bool>
{ Public
:
    Tablecomparefuctorwithadapter () {}
    ~tablecomparefuctorwithadapter () {}
    
    bool  operator () (const Stableinfor value, const stableinfor fixedvalue) Const
    {
        if (Value.m_stableid = = Fixedvalue.m_stableid) 
        { C10/>return true;
        }
        else
        {
            return  false;
        }}}
;

Finally, call the FIND_IF algorithm: include the header file first:

#import "algorithm"

using namespace std;

   Std::vector<stableinfor>::iterator  iter_begin =  tableinforlist.begin ();
    Std::vector<stableinfor>::iterator  iter_end = Tableinforlist.end ();
    
    Tablecomparefuctorwithadapter  Comparefuctor;
    Std::vector<stableinfor>::iterator  iter_find = 
    find_if (
            iter_begin,
            iter_end, 
            std: : bind2nd (Comparefuctor, tableinfo.m_tableinfor)
            );

    if (iter_find!= iter_end) 
    {
        tableinforlist.erase (iter_find);
    }

This example also first finds the first qualifying element and then deletes it from the container.





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.