天天看點

STL周遊算法 “for_each ”,“transform”

void ShowEmlmt(int& obj)
{
    cout << obj << " ";
}

void main1()
{
    vector<int> v1;
    v1.push_back();
    v1.push_back();
    v1.push_back();

    for_each(v1.begin(), v1.end(), ShowEmlmt);
    replace(v1.begin(), v1.end(), , );
    for_each(v1.begin(), v1.end(), ShowEmlmt);
    cout << endl;

}
           
int  increase(int i)  
{  
    return i + ;  
}  

// transform函數用法  
void play_transform()  
{  
    vector<int> v1;  
    v1.push_back();  
    v1.push_back();  
    v1.push_back();  

    printV(v1);  
    cout << endl;  

    //transform 使用回調函數  
    transform(v1.begin(), v1.end(), v1.begin(), increase);  
    printV(v1);  
    cout << endl;  

    //transform 使用 預定義的函數對象  
    transform(v1.begin(), v1.end(), v1.begin(), negate<int>());  
    printV(v1);  
    cout << endl;  

    //transform 使用 函數擴充卡 和函數對象  
    list<int> mylist;  
    mylist.resize(v1.size());  

    transform(v1.begin(), v1.end(), mylist.begin(), bind2nd(multiplies<int>(), ));  
    printList(mylist);  
    cout << endl;  

    //transform 也可以把運算結果 直接輸出到螢幕  
    transform(v1.begin(), v1.end(), ostream_iterator<int>(cout, " "), negate<int>());  
    cout << endl;  


           

繼續閱讀