1、STL算法--find_if()

(1)、代碼如下:
#include<iostream>
#include<vector>
#include<algorithm>
#include<functional>
using namespace std;
template<typename Type>
class IsDiv{
    public:
        IsDiv(const Type &divisor){
            this->divisor = divisor;
        }   
        bool operator()(Type &t){
            return t%divisor == 0;
        }   
    protected:
    private:
        Type divisor;
};
int main(void){
    vector<int> v2; 
    for(int i = 10; i < 33; i++){
        v2.push_back(i);
    }   
    int a = 4;
    IsDiv<int> myDiv(a);
    //find_if(v2.begin(), v2.end(), myDiv);
    vector<int>::iterator it;
    it =find_if(v2.begin(), v2.end(), IsDiv<int>(a) );
    if(it == v2.end()){
        cout<<"容器中沒有值是4的元素"<<endl;
    }else{
        cout<<"第一個(gè)被4整除的元素是:"<<*it<<endl;
    }
    return 0;
}(2)、運(yùn)行結(jié)果:

2、STL算法--plus的使用
(1)、代碼如下:
#include<iostream>
#include<vector>
#include<algorithm>
#include<functional>
using namespace std;
//plus 預(yù)定義好的函數(shù)對(duì)象,能實(shí)現(xiàn)不同數(shù)據(jù) + 算法;
//實(shí)現(xiàn)了數(shù)據(jù)類型和算法的分離======》通過函數(shù)對(duì)象技術(shù)實(shí)現(xiàn)的;
//
//思考,怎么知道plus<type>是2個(gè)參數(shù)------>多看看源碼;
void main21(){
    plus<int> intAdd;
    int x = 10;
    int y = 20;
    int z = intAdd(x, y);
    cout<<"z:"<<z<<endl;
    plus<string> stringAdd;
    string s1 = "aaa";
    string s2 = "bbb";
    string s3 = stringAdd(s1, s2);
    cout<<"s3:"<<s3<<endl;
    vector<string> v1;
    v1.push_back("bbb");
    v1.push_back("aaa");
    v1.push_back("ccc");
    v1.push_back("zzz");
    v1.push_back("ccc");
    v1.push_back("ccc");
    sort(v1.begin(), v1.end(), greater<string>()); //降序排列;
    vector<string>::iterator it;
    for(it = v1.begin(); it != v1.end(); it++){
        cout<<*it<<endl;
    }
    //求“ccc”出現(xiàn)的字符串的個(gè)數(shù);
    string sc = "ccc"; //函數(shù)適配器:將函數(shù)和參數(shù)強(qiáng)行綁定;
    //equal_to<string>有2個(gè)參數(shù),left參數(shù)來自容器,right參數(shù)來自sc,
    //bind2nd就是函數(shù)適配器:把預(yù)定義函數(shù)對(duì)象和第二個(gè)參數(shù)進(jìn)行綁定;`
    int num = count_if(v1.begin(), v1.end(), bind2nd(equal_to<string>(), sc));
    cout<<"num:"<<num<<endl;
}
int main(void){
    main21();
    return 0;
}(2)、運(yùn)行結(jié)果:

3、STL算法--for_each()
(1)、代碼如下:
#include<iostream>
#include<vector>
#include<algorithm>
#include<functional>
using namespace std;
void printV(vector<int> &v){
    vector<int>::iterator it;
    for(it = v.begin(); it != v.end(); it++){
        cout<<*it<<" ";
    }
    cout<<endl;
}
void showElem(int &n){
    cout<<n<<" ";
}
class MyShow{
    public:
        MyShow(){
            num = 0;
        }
    void operator()(int &n){
        num++;
        cout<<n<<" ";
    }
    void printNum(){
        cout<<"num :"<<num<<endl;
    }
    private:
        int num;
};
int main(void){
    vector<int> v1;
    v1.push_back(1);
    v1.push_back(3);
    v1.push_back(5);
    printV(v1);
    //第三個(gè)參數(shù)是:函數(shù)對(duì)象/回掉函數(shù)
    //for_each(v1.begin(), v1.end(), showElem);  //利用的是回調(diào)函數(shù) 
    for_each(v1.begin(), v1.end(), MyShow()); //利用的是函數(shù)對(duì)象(這個(gè)類中重載了())
    //函數(shù)的返回值是函數(shù)對(duì)象
    cout<<endl;    
    MyShow my1 = for_each(v1.begin(), v1.end(), MyShow()); //利用的是函數(shù)對(duì)象(這個(gè)類中重載了())
    my1.printNum();
    return 0;
}(2)、運(yùn)行結(jié)果:

4、for_each()和transform()的區(qū)別
(1)、代碼如下:
#include<iostream>
#include<vector>
#include<algorithm>
#include<functional>
using namespace std;
void showElem(int &n){
    cout<<n<<" ";
} 
int showElem2(int &n){
    cout<<n<<" ";
    return n;
} 
//for_each和transform的本質(zhì)區(qū)別:
//結(jié)論:
//1、一般情況下,for_each所使用的函數(shù)對(duì)象,參數(shù)是引用,沒有返回值;
//2、transform所使用的函數(shù)對(duì)象,參數(shù)一般不使用引用,而是還有返回值;
int main(void){
    vector<int> v1; 
    v1.push_back(1);
    v1.push_back(3);
    v1.push_back(5);
    
    vector<int> v2 = v1; 
    for_each(v1.begin(), v1.end(), showElem);
    transform(v2.begin(), v2.end(), v2.begin(), showElem2);//transform對(duì)回調(diào)函數(shù)的要求;返回值必須有
    cout<<endl;
    return 0;
}運(yùn)行結(jié)果:

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。
                本文題目:find_if(),plus,for_each()的用法-創(chuàng)新互聯(lián)
                
                鏈接分享:http://chinadenli.net/article34/eccse.html
            
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站改版、虛擬主機(jī)、手機(jī)網(wǎng)站建設(shè)、網(wǎng)站建設(shè)、移動(dòng)網(wǎng)站建設(shè)、Google
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容