常用STL算法1_遍歷

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <list>
#include <algorithm>
#include <functional>
#include <string>
#include <iterator> //輸出流
using namespace std;

//函數對象
struct MyShow
{
public:
    MyShow()
    {
        m_count = 0;
    }
    void operator()(const int & elem)
    {
        cout<<elem<<" ";
        m_count++;
    }
    void printCount()
    {
        cout<<"m_count:"<<m_count<<endl;
    }
private:
    int m_count;
};

//一般情況下,for_each所使用的函數對象,參數是引用,沒有返回值
//回調函數
void show(const int &iItem)
{
    cout << iItem << " ";
}
//for_each
void main061_for_each()
{
    int iArray[] = {1,2,3,4,5};
    vector<int> vecInt(iArray,iArray+sizeof(iArray)/sizeof(iArray[0]));
    //1. 回調函數
    for_each(vecInt.begin(), vecInt.end(), show);
    cout<<endl;

    //2. 函數對象
    for_each(vecInt.begin(), vecInt.end(), MyShow());
    cout<<endl;

    //3. 函數對象可以記錄狀態  for_each返回值:_Fn1 for_each(_InIt _First, _InIt _Last, _Fn1 _Func)
    MyShow myshow;
    //for_each(vecInt.begin(), vecInt.end(), myshow);//no_ok
    //myshow = for_each(vecInt.begin(), vecInt.end(), myshow);//ok
    myshow = for_each(vecInt.begin(), vecInt.end(), MyShow());//ok
    cout<<endl;
    cout<<endl;
    myshow.printCount();

    /*
    _Fn1 for_each(_InIt _First, _InIt _Last, _Fn1 _Func)
    {   // perform function for each element
        _DEBUG_RANGE(_First, _Last);
        _DEBUG_POINTER(_Func);
        _For_each(_Unchecked(_First), _Unchecked(_Last), _Func);

        return (_STD move(_Func));
    }
    */
}
//Transform所使用的函數對象,參數一般不使用引用,而是使用元素,並且有返回值
int op_increase2 (int i) { return ++i; }  
int op_sum2 (int i, int j) { return i+j; }  
template <typename T>
void printVectorElem(T &v)
{
    T::iterator it;
    for (it=v.begin(); it!=v.end(); ++it) 
    {
        cout <<*it <<" ";  
    }
    cout << endl; 
}
//transform 兩種應用
void main062_transform()
{
    vector<int> first;  
    vector<int> second;  
    vector<int>::iterator it;  

    // 1.set some values:  
    for (int i=1; i<6; i++)
    {
        first.push_back (i*10); //  first: 10 20 30 40 50  
    }

    second.resize(first.size());     // allocate space  

    //2. 一元函數 回調
    //_OutIt transform(_InIt _First, _InIt _Last, _OutIt _Dest, _Fn1 _Func)
    // transform [_First, _Last) with _Func
    transform (first.begin(), first.end(), second.begin(), op_increase2);  //回調
    // second: 11 21 31 41 51  

    //2. 二元函數 回調
    //_OutIt transform(_InIt1 _First1, _InIt1 _Last1,_InIt2 _First2, _OutIt _Dest, _Fn2 _Func)
    // transform [_First1, _Last1) and [_First2, ...) with _Func
    transform (first.begin(), first.end(), second.begin(), first.begin(), op_sum2); //回調
    //  first: 21 41 61 81 101  

    cout << "first contains:";  
    printVectorElem(first);;  

    //3. 預定義函數對象
    /*
    struct negate: public unary_function<_Ty, _Ty>
    {   // functor for unary operator-
        _Ty operator()(const _Ty& _Left) const
        {   // apply operator- to operand
            return (-_Left);
        }
    };
    */
    transform(first.begin(), first.end(), first.begin(), negate<int>());
    cout<<"負數:";
    printVectorElem(first);

    //4. 函數適配器
    list<int> list1;
    list1.resize(first.size());
    /*
    struct multiplies: public binary_function<_Ty, _Ty, _Ty>
    {   
        // functor for operator*
        _Ty operator()(const _Ty& _Left, const _Ty& _Right) const
        {   
            // apply operator* to operands
            return (_Left * _Right);
        }
    };
    */
    transform(first.begin(), first.end(), list1.begin(), bind2nd(multiplies<int>(),10));
    cout<<"適配器(x10):";
    printVectorElem(list1);

    //5 輸出到屏幕  ostream_iterator<int>(cout, " ")
    transform(first.begin(),first.end(),ostream_iterator<int>(cout, " "), negate<int>());


}

void main063_for_each_pk_transform()
{
    //一般情況下,for_each所使用的函數對象,參數是引用,沒有返回值
    //Transform所使用的函數對象,參數一般不使用引用,而是使用元素,並且有返回值

    /* transform()所需要的函數對象要求有返回值*/
    /*
    _OutIt _Transform(_InIt _First, _InIt _Last, _OutIt _Dest, _Fn1 _Func)
    {   
        // transform [_First, _Last) with _Func
        for (; _First != _Last; ++_First, ++_Dest)
        {
            *_Dest = _Func(*_First);//返回值
        }
        return (_Dest);
    }
    */

}
int main()
{
    //main061_for_each();
    main062_transform();
    main063_for_each_pk_transform();
    cout<<"\nhello"<<endl;
    system("pause");
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章