클래스 템플릿과 함수 대상

4014 단어 struct함수.대상
# include <iostream>
# include <vector>
# include <list>
# include <algorithm>

using namespace std;

//class absInt{
// public :
// int operator() (int val){
//
// }
//};

// 
struct absInt{
    // : 
    int operator() (int val){
        return val<0?-val:val;
    }
};

// 
template<typename elementType>
void FuncDisplayElement(const elementType & element){

    cout<<element<<" ";
}

// 
template<typename elementType>
struct DisplayElement{

    // 
    int m_nCount;
     DisplayElement(){
        m_nCount = 0;
     }

    void operator() (const elementType & element) /*const  const */{
        m_nCount++;
        cout<<element<<" ";
    }
};

int main(){

    absInt absObj;// 
    int i = -52;
    int j = absObj(i);//  , 
    cout<<j<<endl;

    vector<int> a;
    for(int k=0;k<10;k++){
        a.push_back(k);
    }

    list<char> b;
    for(char c='a';c<'k';c++){
        b.push_back(c);
    }

    //STL 
    // 1.for_each , a.begin , a.end ,  DisplayElement  
    for_each(a.begin(),a.end(),DisplayElement<int>());
    cout<<endl<<endl;
    for_each(b.begin(),b.end(),DisplayElement<char>()); 
    cout<<endl<<endl;
    DisplayElement<int> mResult;
    mResult = for_each(a.begin(),a.end(),mResult);//  
    cout<<mResult.m_nCount<<endl<<endl; 




    return 0;
}

좋은 웹페이지 즐겨찾기